From 4d09ebcaec99d760908834e3860d654819752d76 Mon Sep 17 00:00:00 2001 From: Juha Niskanen Date: Wed, 26 Apr 2017 10:32:20 -0600 Subject: [PATCH] examples: random: avoid stack overflows --- examples/random/Makefile | 2 +- examples/random/random_main.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/random/Makefile b/examples/random/Makefile index 5e4231259..78ca31d91 100644 --- a/examples/random/Makefile +++ b/examples/random/Makefile @@ -39,7 +39,7 @@ APPNAME = rand PRIORITY = SCHED_PRIORITY_DEFAULT -STACKSIZE = 1024 +STACKSIZE = 2048 ASRCS = CSRCS = diff --git a/examples/random/random_main.c b/examples/random/random_main.c index 6abcaaa97..f3d6cbffe 100644 --- a/examples/random/random_main.c +++ b/examples/random/random_main.c @@ -92,12 +92,16 @@ int rand_main(int argc, char *argv[]) if (argc > 1) { - nsamples = atoi(argv[1]); + nsamples = atoi(argv[1]); } /* Clip the number of samples to the configured buffer size */ - if (nsamples > CONFIG_EXAMPLES_MAXSAMPLES) + if (nsamples < 0) + { + nsamples = 0; + } + else if (nsamples > CONFIG_EXAMPLES_MAXSAMPLES) { nsamples = CONFIG_EXAMPLES_MAXSAMPLES; } @@ -124,14 +128,14 @@ int rand_main(int argc, char *argv[]) if (nread < 0) { int errcode = errno; - fprintf(stderr, "ERROR: Read from /dev/randon failed: %d\n", errcode); + fprintf(stderr, "ERROR: Read from /dev/random failed: %d\n", errcode); (void)close(fd); exit(EXIT_FAILURE); } if (nread != nsamples * sizeof(uint32_t)) { - fprintf(stderr, "ERROR: Read from /dev/randon only produced %d bytes\n", + fprintf(stderr, "ERROR: Read from /dev/random only produced %d bytes\n", (int)nread); (void)close(fd); exit(EXIT_FAILURE);