Skip to contentSkip to conversation

Fix gcc warning

PATCH In review Daniel Wagner 2 participants 9 messages Aug 19, 2019 – Aug 23, 2019

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index a04fc49872bf..3ce799bd6319 100644
@@ -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
#2 lore raw

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 | #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
index a04fc49872bf..3ce799bd6319 100644
@@ -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

#3 lore raw

PATCH 2/4 sigwaittest: Increase buffer to avoid overflow

In reply to 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 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
index 59f28a5babcb..2d0c04132fa7 100644
@@ -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
#4 lore raw

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 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
index 59f28a5babcb..2d0c04132fa7 100644
@@ -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
#5 lore raw

PATCH 3/4 svsematest: Increase buffer to avoid overflow

In reply to 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 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
index 8f880786ec0f..15e36af76288 100644
@@ -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
#6 lore raw

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 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
index 8f880786ec0f..15e36af76288 100644
@@ -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
#7 lore raw

PATCH 4/4 deadline_test: Increase buffer to avoid overflow

In reply to 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 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
index e2898de328bb..5b5f40dbb74d 100644
@@ -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
#8 lore raw

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 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
index e2898de328bb..5b5f40dbb74d 100644
@@ -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