Skip to content

Commit

Permalink
compat: Improve attributes R/W functions
Browse files Browse the repository at this point in the history
Check that we have an attribute before trying to read it, to avoid NULL
pointer deferences.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
  • Loading branch information
pcercuei committed Nov 28, 2023
1 parent 993c94c commit c344e85
Showing 1 changed file with 48 additions and 12 deletions.
60 changes: 48 additions & 12 deletions compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,10 @@ ssize_t iio_device_attr_read(const struct iio_device *dev,
const struct iio_attr *attr;

attr = IIO_CALL(iio_device_find_attr)(dev, name);
return IIO_CALL(iio_attr_read_raw)(attr, dst, len);
if (attr)
return IIO_CALL(iio_attr_read_raw)(attr, dst, len);

return -ENOENT;
}

int iio_device_attr_read_bool(const struct iio_device *dev,
Expand All @@ -981,7 +984,10 @@ int iio_device_attr_read_bool(const struct iio_device *dev,
const struct iio_attr *attr;

attr = IIO_CALL(iio_device_find_attr)(dev, name);
return IIO_CALL(iio_attr_read_bool)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_bool)(attr, val);

return -ENOENT;
}

int iio_device_attr_read_longlong(const struct iio_device *dev,
Expand All @@ -990,7 +996,10 @@ int iio_device_attr_read_longlong(const struct iio_device *dev,
const struct iio_attr *attr;

attr = IIO_CALL(iio_device_find_attr)(dev, name);
return IIO_CALL(iio_attr_read_longlong)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_longlong)(attr, val);

return -ENOENT;
}

int iio_device_attr_read_double(const struct iio_device *dev,
Expand All @@ -999,7 +1008,10 @@ int iio_device_attr_read_double(const struct iio_device *dev,
const struct iio_attr *attr;

attr = IIO_CALL(iio_device_find_attr)(dev, name);
return IIO_CALL(iio_attr_read_double)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_double)(attr, val);

return -ENOENT;
}

int iio_device_attr_read_all(struct iio_device *dev,
Expand Down Expand Up @@ -1159,7 +1171,10 @@ ssize_t iio_device_debug_attr_read(const struct iio_device *dev,
const struct iio_attr *attr;

attr = IIO_CALL(iio_device_find_debug_attr)(dev, name);
return IIO_CALL(iio_attr_read_raw)(attr, dst, len);
if (attr)
return IIO_CALL(iio_attr_read_raw)(attr, dst, len);

return -ENOENT;
}

int iio_device_debug_attr_read_bool(const struct iio_device *dev,
Expand All @@ -1168,7 +1183,10 @@ int iio_device_debug_attr_read_bool(const struct iio_device *dev,
const struct iio_attr *attr;

attr = IIO_CALL(iio_device_find_debug_attr)(dev, name);
return IIO_CALL(iio_attr_read_bool)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_bool)(attr, val);

return -ENOENT;
}

int iio_device_debug_attr_read_longlong(const struct iio_device *dev,
Expand All @@ -1177,7 +1195,10 @@ int iio_device_debug_attr_read_longlong(const struct iio_device *dev,
const struct iio_attr *attr;

attr = IIO_CALL(iio_device_find_debug_attr)(dev, name);
return IIO_CALL(iio_attr_read_longlong)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_longlong)(attr, val);

return -ENOENT;
}

int iio_device_debug_attr_read_double(const struct iio_device *dev,
Expand All @@ -1186,7 +1207,10 @@ int iio_device_debug_attr_read_double(const struct iio_device *dev,
const struct iio_attr *attr;

attr = IIO_CALL(iio_device_find_debug_attr)(dev, name);
return IIO_CALL(iio_attr_read_double)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_double)(attr, val);

return -ENOENT;
}

int iio_device_debug_attr_read_all(struct iio_device *dev,
Expand Down Expand Up @@ -1667,7 +1691,10 @@ ssize_t iio_channel_attr_read(const struct iio_channel *chn,
const struct iio_attr *attr;

attr = IIO_CALL(iio_channel_find_attr)(chn, name);
return IIO_CALL(iio_attr_read_raw)(attr, dst, len);
if (attr)
return IIO_CALL(iio_attr_read_raw)(attr, dst, len);

return -ENOENT;
}

int iio_channel_attr_read_bool(const struct iio_channel *chn,
Expand All @@ -1676,7 +1703,10 @@ int iio_channel_attr_read_bool(const struct iio_channel *chn,
const struct iio_attr *attr;

attr = IIO_CALL(iio_channel_find_attr)(chn, name);
return IIO_CALL(iio_attr_read_bool)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_bool)(attr, val);

return -ENOENT;
}

int iio_channel_attr_read_longlong(const struct iio_channel *chn,
Expand All @@ -1685,7 +1715,10 @@ int iio_channel_attr_read_longlong(const struct iio_channel *chn,
const struct iio_attr *attr;

attr = IIO_CALL(iio_channel_find_attr)(chn, name);
return IIO_CALL(iio_attr_read_longlong)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_longlong)(attr, val);

return -ENOENT;
}

int iio_channel_attr_read_double(const struct iio_channel *chn,
Expand All @@ -1694,7 +1727,10 @@ int iio_channel_attr_read_double(const struct iio_channel *chn,
const struct iio_attr *attr;

attr = IIO_CALL(iio_channel_find_attr)(chn, name);
return IIO_CALL(iio_attr_read_double)(attr, val);
if (attr)
return IIO_CALL(iio_attr_read_double)(attr, val);

return -ENOENT;
}

int iio_channel_attr_read_all(struct iio_channel *chn,
Expand Down

0 comments on commit c344e85

Please sign in to comment.