Skip to content

Commit

Permalink
./tests/iio_attr : move from allocating space on stack to malloc
Browse files Browse the repository at this point in the history
Dan pointed out that #268 was mostly because iio_attr didn't allocate
enough room in it's internal buffers for some large kernel attributes.

To resolve this, move the 1k stack allocations to 16k heap allocations
via using malloc/free. This patch changes all potential buffers to
a malloc. This only effects the test utility iio_attr, not the core
library.

Signed-off-by: Robin Getz <[email protected]>
  • Loading branch information
rgetz committed Jan 19, 2020
1 parent 0a48e71 commit cc9b3c7
Showing 1 changed file with 48 additions and 20 deletions.
68 changes: 48 additions & 20 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ enum backend {
AUTO
};

/*
* internal buffers need to be big enough for attributes
* coming back from the kernel. Because of virtual memory,
* only the amount of ram that is needed is used.
*/
#define BUF_SIZE 16384

static void * xmalloc(size_t n)
{
void *p = malloc(n);

if (!p && n != 0) {
fprintf(stderr, MY_NAME
" fatal error: allocating %zu bytes failed\n",n);
exit(EXIT_FAILURE);
}

return p;
}

static bool str_match(const char * haystack, char * needle, bool ignore)
{
bool ret = false;
Expand Down Expand Up @@ -112,9 +132,10 @@ static struct iio_context * autodetect_context(void)

ret = iio_scan_context_get_info_list(scan_ctx, &info);
if (ret < 0) {
char err_str[1024];
iio_strerror(-ret, err_str, sizeof(err_str));
char *err_str = xmalloc(BUF_SIZE);
iio_strerror(-ret, err_str, BUF_SIZE);
fprintf(stderr, "Scanning for IIO contexts failed: %s\n", err_str);
free (err_str);
goto err_free_ctx;
}

Expand Down Expand Up @@ -148,20 +169,20 @@ static void dump_device_attributes(const struct iio_device *dev,
const char *attr, const char *wbuf, bool quiet)
{
ssize_t ret;
char buf[1024];
char *buf = xmalloc(BUF_SIZE);

if (!wbuf || !quiet) {
if (!quiet)
printf("dev '%s', attr '%s', value :",
iio_device_get_name(dev), attr);
ret = iio_device_attr_read(dev, attr, buf, sizeof(buf));
ret = iio_device_attr_read(dev, attr, buf, BUF_SIZE);
if (ret > 0) {
if (quiet)
printf("%s\n", buf);
else
printf("'%s'\n", buf);
} else {
iio_strerror(-ret, buf, sizeof(buf));
iio_strerror(-ret, buf, BUF_SIZE);
printf("ERROR: %s (%li)\n", buf, (long)ret);
}
}
Expand All @@ -171,22 +192,23 @@ static void dump_device_attributes(const struct iio_device *dev,
if (!quiet)
printf("wrote %li bytes to %s\n", (long)ret, attr);
} else {
iio_strerror(-ret, buf, sizeof(buf));
iio_strerror(-ret, buf, BUF_SIZE);
printf("ERROR: %s (%li) while writing '%s' with '%s'\n",
buf, (long)ret, attr, wbuf);
}
dump_device_attributes(dev, attr, NULL, quiet);
}
free(buf);
}

static void dump_buffer_attributes(const struct iio_device *dev,
const char *attr, const char *wbuf, bool quiet)
{
ssize_t ret;
char buf[1024];
char *buf = xmalloc(BUF_SIZE);

if (!wbuf || !quiet) {
ret = iio_device_buffer_attr_read(dev, attr, buf, sizeof(buf));
ret = iio_device_buffer_attr_read(dev, attr, buf, BUF_SIZE);

if (!quiet)
printf("dev '%s', buffer attr '%s', value :",
Expand All @@ -198,7 +220,7 @@ static void dump_buffer_attributes(const struct iio_device *dev,
else
printf("'%s'\n", buf);
} else {
iio_strerror(-ret, buf, sizeof(buf));
iio_strerror(-ret, buf, BUF_SIZE);
printf("ERROR: %s (%li)\n", buf, (long)ret);
}
}
Expand All @@ -209,22 +231,24 @@ static void dump_buffer_attributes(const struct iio_device *dev,
if (!quiet)
printf("wrote %li bytes to %s\n", (long)ret, attr);
} else {
iio_strerror(-ret, buf, sizeof(buf));
iio_strerror(-ret, buf, BUF_SIZE);
printf("ERROR: %s (%li) while writing '%s' with '%s'\n",
buf, (long)ret, attr, wbuf);
}
dump_buffer_attributes(dev, attr, NULL, quiet);
}

free(buf);
}

static void dump_debug_attributes(const struct iio_device *dev,
const char *attr, const char *wbuf, bool quiet)
{
ssize_t ret;
char buf[1024];
char *buf = xmalloc(BUF_SIZE);

if (!wbuf || !quiet) {
ret = iio_device_debug_attr_read(dev, attr, buf, sizeof(buf));
ret = iio_device_debug_attr_read(dev, attr, buf, BUF_SIZE);

if (!quiet)
printf("dev '%s', debug attr '%s', value :",
Expand All @@ -236,7 +260,7 @@ static void dump_debug_attributes(const struct iio_device *dev,
else
printf("'%s'\n", buf);
} else {
iio_strerror(-ret, buf, sizeof(buf));
iio_strerror(-ret, buf, BUF_SIZE);
printf("ERROR: %s (%li)\n", buf, (long)ret);
}
}
Expand All @@ -247,19 +271,21 @@ static void dump_debug_attributes(const struct iio_device *dev,
if (!quiet)
printf("wrote %li bytes to %s\n", (long)ret, attr);
} else {
iio_strerror(-ret, buf, sizeof(buf));
iio_strerror(-ret, buf, BUF_SIZE);
printf("ERROR: %s (%li) while writing '%s' with '%s'\n",
buf, (long)ret, attr, wbuf);
}
dump_debug_attributes(dev, attr, NULL, quiet);
}

free(buf);
}

static void dump_channel_attributes(const struct iio_device *dev,
struct iio_channel *ch, const char *attr, const char *wbuf, bool quiet)
{
ssize_t ret;
char buf[1024];
char *buf = xmalloc(BUF_SIZE);
const char *type_name;

if (!wbuf || !quiet) {
Expand All @@ -268,7 +294,7 @@ static void dump_channel_attributes(const struct iio_device *dev,
else
type_name = "input";

ret = iio_channel_attr_read(ch, attr, buf, sizeof(buf));
ret = iio_channel_attr_read(ch, attr, buf, BUF_SIZE);
if (!quiet)
printf("dev '%s', channel '%s' (%s), ",
iio_device_get_name(dev),
Expand All @@ -286,7 +312,7 @@ static void dump_channel_attributes(const struct iio_device *dev,
else
printf("value '%s'\n", buf);
} else {
iio_strerror(-ret, buf, sizeof(buf));
iio_strerror(-ret, buf, BUF_SIZE);
printf("ERROR: %s (%li)\n", buf, (long)ret);
}
}
Expand All @@ -296,12 +322,13 @@ static void dump_channel_attributes(const struct iio_device *dev,
if (!quiet)
printf("wrote %li bytes to %s\n", (long)ret, attr);
} else {
iio_strerror(-ret, buf, sizeof(buf));
iio_strerror(-ret, buf, BUF_SIZE);
printf("error %s (%li) while writing '%s' with '%s'\n",
buf, (long)ret, attr, wbuf);
}
dump_channel_attributes(dev, ch, attr, NULL, quiet);
}
free(buf);
}

static const struct option options[] = {
Expand Down Expand Up @@ -545,11 +572,12 @@ int main(int argc, char **argv)

if (!ctx) {
if (!detect_context) {
char buf[1024];
char *buf = xmalloc(BUF_SIZE);

iio_strerror(errno, buf, sizeof(buf));
iio_strerror(errno, buf, BUF_SIZE);
fprintf(stderr, "Unable to create IIO context: %s\n",
buf);
free(buf);
}

return EXIT_FAILURE;
Expand Down

0 comments on commit cc9b3c7

Please sign in to comment.