Skip to contentSkip to conversation

IIO: Documentation: generic_buffer example: Avoid NULL pointer dereference

PATCH In review [email protected] 2 participants 8 messages Feb 24, 2011

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

PATCH 1/6 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 771b236..3cc18ab 100644
@@ -168,6 +168,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) {

--
1.6.0.2

#1 lore raw

PATCH 2/6 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,

--
1.6.0.2

#2 lore raw

PATCH 3/6 IIO: Documentation: iio_utils: style consistency fix

In reply to [email protected]

From: Michael Hennerich <[email protected]>

No functional changes

---
 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;

--
1.6.0.2

#3 lore raw

PATCH 4/6 IIO: Documentation: iio_utils: Prevent buffer overflow

In reply to [email protected]

From: Michael Hennerich <[email protected]>

The first part of build_channel_array()identifies the number of enabled channels. Further down this count is used to allocate the ci_array. The next section parses the scan_elements directory again, and fills ci_array regardless if the channel is enabled or not. So if less than available channels are enabled ci_array memory is overflowed.

This fix makes sure that we allocate enough memory. But the whole approach looks a bit cumbersome to me. Why not allocate memory for MAX_CHANNLES, less say 64 (I never seen a part with more than that channels). And skip the first part entirely.

---
 drivers/staging/iio/Documentation/iio_utils.h |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
drivers/staging/iio/Documentation/iio_utils.h +3 -1
index 4b023aa..bde2313 100644
@@ -290,15 +290,17 @@ inline int build_channel_array(const char *device_dir,
 			fscanf(sysfsfp, "%u", &ret);
 			if (ret == 1)
 				(*counter)++;
+			count++;
 			fclose(sysfsfp);
 			free(filename);
 		}
-	*ci_array = malloc(sizeof(**ci_array)*(*counter));
+	*ci_array = malloc(sizeof(**ci_array)*count);
 	if (*ci_array == NULL) {
 		ret = -ENOMEM;
 		goto error_close_dir;
 	}
 	seekdir(dp, 0);
+	count = 0;
 	while (ent = readdir(dp), ent != NULL) {
 		if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
 			   "_en") == 0) {

--
1.6.0.2

#4 lore raw

PATCH 5/6 IIO: Documentation: iio_utils: fix mask generation

In reply to [email protected]

From: Michael Hennerich <[email protected]>

Variable sizeint is used uninitialized. Remove sizeint completely and use bits_used instead.

---
 drivers/staging/iio/Documentation/iio_utils.h |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/staging/iio/Documentation/iio_utils.h +2 -2
index bde2313..1b33c04 100644
@@ -113,7 +113,7 @@ inline int iioutils_get_type(unsigned *is_signed,
 	DIR *dp;
 	char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
 	char signchar;
-	unsigned sizeint, padint;
+	unsigned padint;
 	const struct dirent *ent;
 
 	ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
@@ -159,7 +159,7 @@ inline int iioutils_get_type(unsigned *is_signed,
 			fscanf(sysfsfp,
 			       "%c%u/%u", &signchar, bits_used, &padint);
 			*bytes = padint / 8;
-			if (sizeint == 64)
+			if (*bits_used == 64)
 				*mask = ~0;
 			else
 				*mask = (1 << *bits_used) - 1;

--
1.6.0.2

#6 lore raw

Greg KH wrote on 2011-02-24:

Quoted 10 lines
Quoted 1 line · On Thu, Feb 24, 2011 at 04:34:50PM +0100, [email protected] wrote:

From: Michael Hennerich <[email protected]>

I only received 5 patches here, not six, missing the 6/6 patch. Did it get lost somewhere?

thanks,

greg k-h

No - I temporarily restrained it - Sorry I shouldn't made it part of the se= quence.
[PATCH 6/6] IIO: Documentation: iio_utils: fix channel array generation.

Was not yet Acked-by Jonathan and I got indication that he likes to test it=
and or
try to push it's alternative implementation.

Greetings,
Michael

--
Analog Devices GmbH Wilhelm-Wagenfeld-Str. 6 80807 Muenchen
Sitz der Gesellschaft: Muenchen; Registergericht: Muenchen HRB 40368; Gesch= aeftsfuehrer:Dr.Carsten Suckrow, Thomas Wessel, William A. Martin, Margaret=
Seif

#7 lore raw
Quoted 10 lines · Michael

Greg KH wrote on 2011-02-24:

Quoted 9 lines
Quoted 1 line · On Thu, Feb 24, 2011 at 04:34:50PM +0100, [email protected] wrote:

From: Michael Hennerich <[email protected]>

I only received 5 patches here, not six, missing the 6/6 patch. Did it get lost somewhere?

thanks,

Quoted 7 lines
Quoted 1 line

greg k-h

No - I temporarily restrained it - Sorry I shouldn't made it part of the sequence. [PATCH 6/6] IIO: Documentation: iio_utils: fix channel array generation.

Was not yet Acked-by Jonathan and I got indication that he likes to test it and or try to push it's alternative implementation.

Ok, just worried I missed a patch somewhere, thanks for the response.

greg k-h