Fix gcc warning
cover Fix gcc warning
On F29 and F30 gcc is warning about too small buffers. Let's fix this.
changes v1:
- Increase the buffer to needed exact size as pointed out by Sebastian
Daniel Wagner (4):
pmqtest: Increase buffer to avoid overflow
sigwaittest: Increase buffer to avoid overflow
svsematest: Increase buffer to avoid overflow
deadline_test: Increase buffer to avoid overflow
src/pmqtest/pmqtest.c | 4 ++-- src/sched_deadline/deadline_test.c | 2 +- src/sigwaittest/sigwaittest.c | 2 +- src/svsematest/svsematest.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-)
Signature · 1 line
2.21.0
PATCH 1/4 pmqtest: Increase buffer to avoid overflow
Increase the size of the char buffer. gcc 9.1.1 reports:
src/pmqtest/pmqtest.c: In function ‘main’:src/pmqtest/pmqtest.c:46:21: warning: ‘%d’ directive writing between 1 and 10 bytes into a region of size 8 [-Wformat-overflow=]
46 | #define SYNCMQ_NAME "/syncmsg%d"
| ^~~~~~~~~~~~
src/pmqtest/pmqtest.c:445:3: note: ‘sprintf’ output between 10 and 19 bytes into a destination of size 16
445 | sprintf(mqname, SYNCMQ_NAME, i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- src/pmqtest/pmqtest.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
src/pmqtest/pmqtest.c +2 -2
@@ -440,7 +440,7 @@ int main(int argc, char *argv[]) goto nomem;for (i =0 ; i < num_threads; i++) { -char mqname[16 ]; +char mqname[19 ];sprintf (mqname,SYNCMQ_NAME , i); receiver[i].syncmq =mq_open (mqname, oflag,0777 , &mqstat);
@@ -567,7 +567,7 @@ int main(int argc, char *argv[]) }nanosleep (&maindelay,NULL );for (i =0 ; i < num_threads; i++) { -char mqname[16 ]; +char mqname[19 ];mq_close (receiver[i].syncmq);sprintf (mqname,SYNCMQ_NAME , i);
Signature · 1 line
2.21.0
Re: [PATCH rt-tests v1 v1 1/4] pmqtest: Increase buffer to avoid overflow
Quoted 41 lines · Daniel Wagner
Increase the size of the char buffer. gcc 9.1.1 reports:
src/pmqtest/pmqtest.c: In function ‘main’:src/pmqtest/pmqtest.c:46:21: warning: ‘%d’ directive writing between 1 and 10 bytes into a region of size 8 [-Wformat-overflow=]
46 | #defineSYNCMQ_NAME"/syncmsg%d"| ^~~~~~~~~~~~
src/pmqtest/pmqtest.c:445:3: note: ‘sprintf’ output between 10 and 19 bytes into a destination of size 16445 | sprintf(mqname, SYNCMQ_NAME, i); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Signed-off-by Daniel Wagner <[email protected]>--- src/pmqtest/pmqtest.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)src/pmqtest/pmqtest.c +2
-2@@ -440,7 +440,7 @@ int main(int argc, char *argv[]) goto nomem;for (i =0 ; i < num_threads; i++) { -char mqname[16 ]; +char mqname[19 ];sprintf (mqname,SYNCMQ_NAME , i); receiver[i].syncmq =mq_open (mqname, oflag,0777 , &mqstat);@@ -567,7 +567,7 @@ int main(int argc, char *argv[]) }nanosleep (&maindelay,NULL );for (i =0 ; i < num_threads; i++) { -char mqname[16 ]; +char mqname[19 ];mq_close (receiver[i].syncmq);sprintf (mqname,SYNCMQ_NAME , i);Signature · 1 line
2.21.0
I don't love the use of "magic numbers". Also the compiler considers the signed integers to be −2147483648 to 2147483647 so including the sign that is potentially up to 11 chars, plus our string is "/syncmsg" is 8 chars so that's where the 19 comes from. However we are using the int to represent threads, so we know we can't have a negative number. However sprintf also adds '\n' which brings us back to 19 again anyway.
This is better than what we have, so
PATCH 2/4 sigwaittest: Increase buffer to avoid overflow
In reply to Daniel WagnerIncrease the size of the char buffer. gcc 9.1.1 reports:
src/sigwaittest/sigwaittest.c:494:5: note: ‘sprintf’ output between 5 and 14 bytes into a destination of size 8
494 | sprintf(f_opt, "-fr%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~src/sigwaittest/sigwaittest.c:522:24: warning: ‘%d’ directive writing between 1 and 10 bytes into a region of size 5 [-Wformat-overflow=]
522 | sprintf(f_opt, "-fs%d", i);
| ^~
--- src/sigwaittest/sigwaittest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/sigwaittest/sigwaittest.c +1 -1
@@ -352,7 +352,7 @@ int main(int argc, char *argv[])struct params *sender =NULL ;sigset_t sigset;void *param =NULL ; -char f_opt[8 ]; +char f_opt[14 ];struct timespec launchdelay, maindelay;process_options (argc, argv);
Signature · 1 line
2.21.0
Re: [PATCH rt-tests v1 v1 2/4] sigwaittest: Increase buffer to avoid overflow
Quoted 30 lines · Daniel Wagner
Increase the size of the char buffer. gcc 9.1.1 reports:
src/sigwaittest/sigwaittest.c:494:5: note: ‘sprintf’ output between 5 and 14 bytes into a destination of size 8494 | sprintf(f_opt, "-fr%d", i); | ^~~~~~~~~~~~~~~~~~~~~~~~~~
src/sigwaittest/sigwaittest.c:522:24: warning: ‘%d’ directive writing between 1 and 10 bytes into a region of size 5 [-Wformat-overflow=]
522 |sprintf(f_opt, "-fs%d", i);
| ^~Signed-off-by Daniel Wagner <[email protected]>--- src/sigwaittest/sigwaittest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)src/sigwaittest/sigwaittest.c +1
-1@@ -352,7 +352,7 @@ int main(int argc, char *argv[])struct params *sender =NULL ;sigset_t sigset;void *param =NULL ; -char f_opt[8 ]; +char f_opt[14 ];struct timespec launchdelay, maindelay;process_options (argc, argv);Signature · 1 line
2.21.0
PATCH 3/4 svsematest: Increase buffer to avoid overflow
In reply to Daniel WagnerIncrease the size of the char buffer. gcc 9.1.1 reports:
rc/svsematest/svsematest.c:578:24: warning: ‘%d’ directive writing between 1 and 10 bytes into a region of size 5 [-Wformat-overflow=]
578 | sprintf(f_opt, "-fr%d", i);
| ^~src/svsematest/svsematest.c:606:5: note: ‘sprintf’ output between 5 and 14 bytes into a destination of size 8
606 | sprintf(f_opt, "-fs%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~--- src/svsematest/svsematest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/svsematest/svsematest.c +1 -1
@@ -401,7 +401,7 @@ int main(int argc, char *argv[])struct params *sender =NULL ;sigset_t sigset;void *param =NULL ; -char f_opt[8 ]; +char f_opt[14 ];struct timespec launchdelay, maindelay; myfile =getenv ("_" );
Signature · 1 line
2.21.0
Re: [PATCH rt-tests v1 v1 3/4] svsematest: Increase buffer to avoid overflow
Quoted 30 lines · Daniel Wagner
Increase the size of the char buffer. gcc 9.1.1 reports:
rc/svsematest/svsematest.c:578:24: warning: ‘%d’ directive writing between 1 and 10 bytes into a region of size 5 [-Wformat-overflow=]
578 |sprintf(f_opt, "-fr%d", i);
| ^~src/svsematest/svsematest.c:606:5: note: ‘sprintf’ output between 5 and 14 bytes into a destination of size 8606 | sprintf(f_opt, "-fs%d", i); | ^~~~~~~~~~~~~~~~~~~~~~~~~~Signed-off-by Daniel Wagner <[email protected]>--- src/svsematest/svsematest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)src/svsematest/svsematest.c +1
-1@@ -401,7 +401,7 @@ int main(int argc, char *argv[])struct params *sender =NULL ;sigset_t sigset;void *param =NULL ; -char f_opt[8 ]; +char f_opt[14 ];struct timespec launchdelay, maindelay; myfile =getenv ("_" );Signature · 1 line
2.21.0
PATCH 4/4 deadline_test: Increase buffer to avoid overflow
In reply to Daniel WagnerIncrease the size of the char buffer. gcc 9.1.1 reports:
src/sched_deadline/deadline_test.c:1803:24: warning: ‘%d’ directive writing between 1 and 11 bytes into a region of size 10 [-Wformat-overflow=]
1803 | sprintf(setcpu_buf, "%d", cpu_count - 1);
| ^~src/sched_deadline/deadline_test.c:1803:23: note: directive argument in the range [-2147483648, 2147483646]
1803 | sprintf(setcpu_buf, "%d", cpu_count - 1);
| ^~~~src/sched_deadline/deadline_test.c:1803:3: note: ‘sprintf’ output between 2 and 12 bytes into a destination of size 10
1803 | sprintf(setcpu_buf, "%d", cpu_count - 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~--- src/sched_deadline/deadline_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/sched_deadline/deadline_test.c +1 -1
@@ -1795,7 +1795,7 @@ int main (int argc, char **argv)/* -b has us bind to the last CPU. */ if (!all_cpus && !setcpu) { - setcpu_buf =malloc (10 ); + setcpu_buf =malloc (12 );if (!setcpu_buf) {perror ("malloc" );exit (-1 );
Signature · 1 line
2.21.0
Re: [PATCH rt-tests v1 v1 4/4] deadline_test: Increase buffer to avoid overflow
Quoted 33 lines · Daniel Wagner
Increase the size of the char buffer. gcc 9.1.1 reports:
src/sched_deadline/deadline_test.c:1803:24: warning: ‘%d’ directive writing between 1 and 11 bytes into a region of size 10 [-Wformat-overflow=]
1803 |sprintf(setcpu_buf, "%d", cpu_count - 1);
| ^~src/sched_deadline/deadline_test.c:1803:23: note: directive argument in the range [-2147483648, 2147483646]
1803 |sprintf(setcpu_buf, "%d", cpu_count - 1);
| ^~~~src/sched_deadline/deadline_test.c:1803:3: note: ‘sprintf’ output between 2 and 12 bytes into a destination of size 101803 | sprintf(setcpu_buf, "%d", cpu_count - 1); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Signed-off-by Daniel Wagner <[email protected]>--- src/sched_deadline/deadline_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)src/sched_deadline/deadline_test.c +1
-1@@ -1795,7 +1795,7 @@ int main (int argc, char **argv)/* -b has us bind to the last CPU. */ if (!all_cpus && !setcpu) { - setcpu_buf =malloc (10 ); + setcpu_buf =malloc (12 );if (!setcpu_buf) {perror ("malloc" );exit (-1 );Signature · 1 line
2.21.0