xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: Wei Liu <wei.liu2@citrix.com>, Ian Jackson <Ian.Jackson@eu.citrix.com>
Subject: [PATCH 2/2] xenpmd: make 32 bit gcc 8.1 non-debug build work
Date: Thu, 26 Jul 2018 15:58:54 +0100	[thread overview]
Message-ID: <20180726145854.10978-3-wei.liu2@citrix.com> (raw)
In-Reply-To: <20180726145854.10978-1-wei.liu2@citrix.com>

32 bit gcc 8.1 non-debug build yields:

xenpmd.c:354:23: error: '%02x' directive output may be truncated writing between 2 and 8 bytes into a region of size 3 [-Werror=format-truncation=]
     snprintf(val, 3, "%02x",
                       ^~~~
xenpmd.c:354:22: note: directive argument in the range [40, 2147483778]
     snprintf(val, 3, "%02x",
                      ^~~~~~
xenpmd.c:354:5: note: 'snprintf' output between 3 and 9 bytes into a destination of size 3
     snprintf(val, 3, "%02x",
     ^~~~~~~~~~~~~~~~~~~~~~~~
              (unsigned int)(9*4 +
              ~~~~~~~~~~~~~~~~~~~~
                             strlen(info->model_number) +
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                             strlen(info->serial_number) +
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                             strlen(info->battery_type) +
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                             strlen(info->oem_info) + 4));
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

All info->* used in calculation are 32 bytes long, and the parsing
code makes sure they are null-terminated, so the end result of the
expression won't exceed 255, which should be able to be fit into 3
bytes in hexadecimal format.

Add an assertion to make gcc happy.

Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/xenpmd/xenpmd.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/tools/xenpmd/xenpmd.c b/tools/xenpmd/xenpmd.c
index 56412a9a81..1c801caa71 100644
--- a/tools/xenpmd/xenpmd.c
+++ b/tools/xenpmd/xenpmd.c
@@ -40,6 +40,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <xenstore.h>
+#include <assert.h>
 
 /* #define RUN_STANDALONE */
 #define RUN_IN_SIMULATE_MODE
@@ -345,18 +346,17 @@ void write_ulong_lsb_first(char *temp_val, unsigned long val)
 void write_battery_info_to_xenstore(struct battery_info *info)
 {
     char val[1024], string_info[256];
+    unsigned int len;
 
     xs_mkdir(xs, XBT_NULL, "/pm");
    
     memset(val, 0, 1024);
     memset(string_info, 0, 256);
     /* write 9 dwords (so 9*4) + length of 4 strings + 4 null terminators */
-    snprintf(val, 3, "%02x", 
-             (unsigned int)(9*4 +
-                            strlen(info->model_number) +
-                            strlen(info->serial_number) +
-                            strlen(info->battery_type) +
-                            strlen(info->oem_info) + 4));
+    len = 9 * 4 + strlen(info->model_number) + strlen(info->serial_number) +
+          strlen(info->battery_type) + strlen(info->oem_info) + 4;
+    assert(len < 255);
+    snprintf(val, 3, "%02x", len);
     write_ulong_lsb_first(val+2, info->present);
     write_ulong_lsb_first(val+10, info->design_capacity);
     write_ulong_lsb_first(val+18, info->last_full_capacity);
-- 
2.11.0


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2018-07-26 14:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-26 14:58 [PATCH 0/2] tools: fix gcc 8.1 build Wei Liu
2018-07-26 14:58 ` [PATCH 1/2] tools: update ipxe changeset Wei Liu
2018-08-20 10:26   ` Ian Jackson
2018-07-26 14:58 ` Wei Liu [this message]
2018-08-20 10:24   ` [PATCH 2/2] xenpmd: make 32 bit gcc 8.1 non-debug build work Ian Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180726145854.10978-3-wei.liu2@citrix.com \
    --to=wei.liu2@citrix.com \
    --cc=Ian.Jackson@eu.citrix.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).