Skip to contentSkip to conversation

IIO: Documentation: generic_buffer example: Avoid NULL pointer dereference

PATCH New [email protected] 2 participants 9 messages Feb 15, 2011 – Feb 22, 2011

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 IIO: Documentation: generic_buffer example: Avoid NULL pointer dereference

From: Michael Hennerich <[email protected]>

In case optarg n is not given return/exit

---
 drivers/staging/iio/Documentation/generic_buffer.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
drivers/staging/iio/Documentation/generic_buffer.c +3 -0
index df23aeb..131d9d0 100644
@@ -151,6 +151,9 @@ int main(int argc, char **argv)
 		}
 	}
 
+	if (device_name == NULL)
+		return -1;
+
 	/* Find the device requested */
 	dev_num = find_type_by_name(device_name, "device");
 	if (dev_num < 0) {
Signature · 1 line
1.6.0.2
#1 lore raw

PATCH 1/1 IIO: Documentation: iio_utils: Avoid double free()

From: Michael Hennerich <[email protected]>

filename is used and freed later

---
 drivers/staging/iio/Documentation/iio_utils.h |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/iio_utils.h +0 -1
index 4dc961c..3cf01a5 100644
@@ -319,7 +319,6 @@ inline int build_channel_array(const char *device_dir,
 			}
 			fscanf(sysfsfp, "%u", &current->enabled);
 			fclose(sysfsfp);
-			free(filename);
 			current->scale = 1.0;
 			current->offset = 0;
 			current->name = strndup(ent->d_name,
Signature · 1 line
1.6.0.2
#3 lore raw

PATCH 1/1 IIO: Documentation: iio_utils: Fix typo

In reply to [email protected]

From: Michael Hennerich <[email protected]>

---
 drivers/staging/iio/Documentation/iio_utils.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/iio_utils.h +1 -1
index 3cf01a5..4b023aa 100644
@@ -374,7 +374,7 @@ inline int build_channel_array(const char *device_dir,
 		}
 	}
 	/* reorder so that the array is in index order*/
-	current = malloc(sizeof(**ci_array)**counter);
+	current = malloc(sizeof(**ci_array)*(*counter));
 	if (current == NULL) {
 		ret = -ENOMEM;
 		goto error_cleanup_array;
Signature · 1 line
1.6.0.2
#5 lore raw

PATCH 1/1 IIO: Documentation: generic_buffer example: scan_size must be bytes_per_datum

In reply to [email protected]

From: Michael Hennerich <[email protected]>

In case a timestamp is present, scan size returned by size_from_channelarray() is not longer valid. However size_from_channelarray() must be still called in order to setup locations within the buffer.

---
 drivers/staging/iio/Documentation/generic_buffer.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/generic_buffer.c +7 -1
index 131d9d0..c382452 100644
@@ -226,7 +226,13 @@ int main(int argc, char **argv)
 	ret = write_sysfs_int("enable", buf_dir_name, 1);
 	if (ret < 0)
 		goto error_free_buf_dir_name;
-	scan_size = size_from_channelarray(infoarray, num_channels);
+
+	scan_size = read_sysfs_posint("bytes_per_datum", buf_dir_name);
+	if (ret < 0)
+		goto error_free_buf_dir_name;
+
+	size_from_channelarray(infoarray, num_channels);
+
 	data = malloc(scan_size*buf_len);
 	if (!data) {
 		ret = -ENOMEM;
Signature · 1 line
1.6.0.2
Acked-by on v1 1/1
#2 lore raw

Re: [PATCH] IIO: Documentation: iio_utils: Avoid double free()

In reply to [email protected]
Quoted 3 lines · On 02/15/11 14:46, [email protected] wrote:

From: Michael Hennerich <[email protected]>

filename is used and freed later

oops. Thanks for this. Should probably go to stable as well as into staging-next.

Quoted 2 lines
Quoted 16 lines
---
 drivers/staging/iio/Documentation/iio_utils.h |    1 -
 1 files changed, 0 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/iio_utils.h +0 -1
index 4dc961c..3cf01a5 100644
@@ -319,7 +319,6 @@ inline int build_channel_array(const char *device_dir,
 			}
 			fscanf(sysfsfp, "%u", &current->enabled);
 			fclose(sysfsfp);
-			free(filename);
 			current->scale = 1.0;
 			current->offset = 0;
 			current->name = strndup(ent->d_name,
#6 lore raw

Re: [PATCH] IIO: Documentation: generic_buffer example: scan_size must be bytes_per_datum

In reply to [email protected]
Quoted 4 lines · On 02/15/11 14:46, [email protected] wrote:

From: Michael Hennerich <[email protected]>

In case a timestamp is present, scan size returned by size_from_channelarray() is not longer valid.

Really? Shouldn't the timestamp just be picked up as another channel assuming all the relevant attributes are present (which they should be...)

Quoted 27 lines

However size_from_channelarray() must be still called in order to setup locations within the buffer.

---
 drivers/staging/iio/Documentation/generic_buffer.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/generic_buffer.c +7 -1
index 131d9d0..c382452 100644
@@ -226,7 +226,13 @@ int main(int argc, char **argv)
 	ret = write_sysfs_int("enable", buf_dir_name, 1);
 	if (ret < 0)
 		goto error_free_buf_dir_name;
-	scan_size = size_from_channelarray(infoarray, num_channels);
+
+	scan_size = read_sysfs_posint("bytes_per_datum", buf_dir_name);
+	if (ret < 0)
+		goto error_free_buf_dir_name;
+
+	size_from_channelarray(infoarray, num_channels);
+
 	data = malloc(scan_size*buf_len);
 	if (!data) {
 		ret = -ENOMEM;
Acked-by on v1 1/1
#4 lore raw

Re: [PATCH] IIO: Documentation: iio_utils: Fix typo

In reply to [email protected]
Quoted 3 lines · On 02/15/11 14:46, [email protected] wrote:

From: Michael Hennerich <[email protected]>

It's not an actual bug, but your version is certainly easier to read, hence the ack. Not worth pushing to stable though and might want to have a description that makes it clear that it is for readability.

Thanks to helpful denizen of another list for providing the following succinct argument. As he put it when I queried a**b...

Reference: http://www.cs.man.ac.uk/~pjj/bnf/c_syntax.bnf

There's no "**" token, so "a**d" has to be parsed as "a" "*" "*" "d".

And there's no postfix "*" operator, so "a" "*" ... can only be a mult_exp. Given it's a mult_exp, the "*" "d" is parsed as a cast_exp, which inevitably means treating it as a unary_exp consisting of a unary_operator ("*") followed by an id which, all by itself, qualifies as a primary_exp, a postfix_exp, a unary_exp and a cast_exp, and is therefore legal as the rhs of a multi_exp.

Quoted 1 line
Quoted 17 lines
---
 drivers/staging/iio/Documentation/iio_utils.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/iio_utils.h +1 -1
index 3cf01a5..4b023aa 100644
@@ -374,7 +374,7 @@ inline int build_channel_array(const char *device_dir,
 		}
 	}
 	/* reorder so that the array is in index order*/
-	current = malloc(sizeof(**ci_array)**counter);
+	current = malloc(sizeof(**ci_array)*(*counter));
 	if (current == NULL) {
 		ret = -ENOMEM;
 		goto error_cleanup_array;
#7 lore raw

Re: [PATCH] IIO: Documentation: generic_buffer example: scan_size must be bytes_per_datum

In reply to Jonathan Cameron
Quoted 10 lines · Jonathan Cameron
Quoted 5 lines · On 02/15/11 14:46, [email protected] wrote:

From: Michael Hennerich <[email protected]>

In case a timestamp is present, scan size returned by size_from_channelarray() is not longer valid.

Really? Shouldn't the timestamp just be picked up as another channel assuming all the relevant attributes are present (which they should be...)

I think the pronunciation is on 'should be'.
Looks like all my drivers don't do it, as well as some of yours.

My assumption was that bytes_per_datum must be properly set. And that the timestamp is always assumed to be the last 8bytes aligned on a 64-bit boundary.
I guess some drivers need fixing.

Quoted 30 lines
Quoted 29 lines

However size_from_channelarray() must be still called in order to setup locations within the buffer.

---
 drivers/staging/iio/Documentation/generic_buffer.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/generic_buffer.c +7 -1
index 131d9d0..c382452 100644
@@ -226,7 +226,13 @@ int main(int argc, char **argv)
      ret = write_sysfs_int("enable", buf_dir_name, 1);
      if (ret < 0)
              goto error_free_buf_dir_name;
-     scan_size = size_from_channelarray(infoarray, num_channels);
+
+     scan_size = read_sysfs_posint("bytes_per_datum", buf_dir_name);
+     if (ret < 0)
+             goto error_free_buf_dir_name;
+
+     size_from_channelarray(infoarray, num_channels);
+
      data = malloc(scan_size*buf_len);
      if (!data) {
              ret = -ENOMEM;
Signature · 8 lines
Greetings,
Michael

--
Analog Devices GmbH      Wilhelm-Wagenfeld-Str. 6      80807 Muenchen
Sitz der Gesellschaft: Muenchen; Registergericht: Muenchen HRB 40368;
Geschaeftsfuehrer:Dr.Carsten Suckrow, Thomas Wessel, William A. Martin,
Margaret Seif
#8 lore raw

Re: [PATCH] IIO: Documentation: generic_buffer example: scan_size must be bytes_per_datum

Quoted 8 lines · Michael Hennerich
Quoted 7 lines · Jonathan Cameron
Quoted 5 lines · On 02/15/11 14:46, [email protected] wrote:

From: Michael Hennerich <[email protected]>

In case a timestamp is present, scan size returned by size_from_channelarray() is not longer valid.

Quoted 7 lines
Quoted 3 lines

Really? Shouldn't the timestamp just be picked up as another channel assuming all the relevant attributes are present (which they should be...)

I think the pronunciation is on 'should be'.
Looks like all my drivers don't do it, as well as some of yours.

My assumption was that bytes_per_datum must be properly set.

That should certainly be the case. We could put a sanity check in to test both numbers are the same as clearly we've gotten it wrong in some drivers.

Quoted 2 lines

And that the timestamp is always assumed to be the last 8bytes aligned on a 64-bit boundary.

It is at the moment but that might change so lets do it right now..

Quoted 1 line

I guess some drivers need fixing.

Yes. It looks that way. Good thing you picked up on this problem.

Quoted 33 lines
Quoted 31 lines
Quoted 30 lines

However size_from_channelarray() must be still called in order to setup locations within the buffer.

---
 drivers/staging/iio/Documentation/generic_buffer.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/generic_buffer.c +7 -1
index 131d9d0..c382452 100644
@@ -226,7 +226,13 @@ int main(int argc, char **argv)
      ret = write_sysfs_int("enable", buf_dir_name, 1);
      if (ret < 0)
              goto error_free_buf_dir_name;
-     scan_size = size_from_channelarray(infoarray, num_channels);
+
+     scan_size = read_sysfs_posint("bytes_per_datum", buf_dir_name);
+     if (ret < 0)
+             goto error_free_buf_dir_name;
+
+     size_from_channelarray(infoarray, num_channels);
+
      data = malloc(scan_size*buf_len);
      if (!data) {
              ret = -ENOMEM;