igt-dev.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* [igt-dev] [PATCH i-g-t v3] intel_reg: Fix truncate string in the snprintf
@ 2018-08-27 11:04 Arkadiusz Hiler
  2018-08-27 11:42 ` [igt-dev] ✓ Fi.CI.BAT: success for " Patchwork
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Arkadiusz Hiler @ 2018-08-27 11:04 UTC (permalink / raw)
  To: igt-dev; +Cc: Rodrigo Siqueira

From: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>

This patch fix the following GCC warning:

../tools/intel_reg.c: In function ‘dump_decode’:
../tools/intel_reg.c:203:41: warning: ‘snprintf’ output may be truncated
before the last format character [-Wformat-truncation=]
   snprintf(decode, sizeof(decode), "\n%s", bin);
[..]
../tools/intel_reg.c:200:40: warning: ‘%s’ directive output may be
truncated writing up to 1023 bytes into a region of size 1022
[-Wformat-truncation=]
    snprintf(decode, sizeof(decode), " (%s)\n%s", tmp, bin);
[..]
../tools/intel_reg.c:200:4: note: ‘snprintf’ output between 5 and 2051
bytes into a destination of size 1024
    snprintf(decode, sizeof(decode), " (%s)\n%s", tmp, bin);
[..]

The decode[] variable contains concatenated contents of bin[] and tmp[],
both of which are allocated as 1024 bytes.

Allocating 1024 chars for bin[] seems like an overkill, since all it
ever holds it the output of to_binary().

to_binary outputs fixed format:
--------------------------------------------------------------------
               24               16                8                0
  1 1 0 1 1 1 1 0  1 0 1 0 1 1 0 1  1 0 1 1 1 1 1 0  1 1 1 0 1 1 1 1
--------------------------------------------------------------------
Which is 138 chars long (sans the new line).

We can limit the size of char bin[] to that number (-ish), and then
slightly bump the size of decode[] to accommodate for combined sizes of
tmp[] and bin[].

Changes since V1:
 - Improve commit message
Changes since V2:
 - updated commit message
 - limit the amount of stack abuse

Signed-off-by: Rodrigo Siqueira <rodrigosiqueiramelo@gmail.com>
Signed-off-by: Arkadiusz Hiler <arkadiusz.hiler@intel.com>
---
 tools/intel_reg.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/intel_reg.c b/tools/intel_reg.c
index ddff2794..1247b70b 100644
--- a/tools/intel_reg.c
+++ b/tools/intel_reg.c
@@ -180,9 +180,9 @@ static void to_binary(char *buf, size_t buflen, uint32_t val)
 
 static void dump_decode(struct config *config, struct reg *reg, uint32_t val)
 {
-	char decode[1024];
+	char decode[1300];
 	char tmp[1024];
-	char bin[1024];
+	char bin[200];
 
 	if (config->binary)
 		to_binary(bin, sizeof(bin), val);
-- 
2.17.1

_______________________________________________
igt-dev mailing list
igt-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/igt-dev

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-09-02 19:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-27 11:04 [igt-dev] [PATCH i-g-t v3] intel_reg: Fix truncate string in the snprintf Arkadiusz Hiler
2018-08-27 11:42 ` [igt-dev] ✓ Fi.CI.BAT: success for " Patchwork
2018-08-27 12:58 ` [igt-dev] ✓ Fi.CI.IGT: " Patchwork
2018-09-02 19:49 ` [igt-dev] [PATCH i-g-t v3] " Rodrigo Siqueira

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).