Skip to contentSkip to conversation

Fix compilation errors on GCC12

PATCH New Tales Aparecida 1 participant 3 messages Jul 14, 2022 objtool

lore
Share & tools
lore
#0 lore raw

cover Fix compilation errors on GCC12

Hello Alex,

I believe you are already working on a rebase right now, but could you please cherry-pick these two commits from torvalds/master to fix compilation errors raised by GCC12 in the meantime?

SHA-1:    82880283d7fcd0a1d20964a56d6d1a5cc0df0713
patch-id: 684ed745d944c90c2aae3c9eda5a4f5aa9cd48e5
SHA-1:    52a9dab6d892763b2a8334a568bd4e2c1a6fde66
patch-id: 6b15e90354234809c3e054332d5d37612c5995dc

Thanks in advance,
Tales

Kees Cook (1):
libsubcmd: Fix use-after-free for realloc(..., 0)

Sergei Trofimovich (1):
objtool: Fix truncated string warning

 tools/lib/subcmd/subcmd-util.h | 11 ++---------
 tools/objtool/check.c          |  2 +-
 2 files changed, 3 insertions(+), 10 deletions(-)
Signature · 1 line
2.37.0
#1 lore raw

PATCH 1/2 objtool: Fix truncated string warning

From: Sergei Trofimovich <[email protected]>

On GCC 12, the build fails due to a possible truncated string:

check.c: In function 'validate_call':
check.c:2865:58: error: '%d' directive output may be truncated writing between 1 and 10 bytes into a region of size 9 [-Werror=format-truncation=]
2865 | snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);
| ^~

In theory it's a valid bug:

    static char pvname[16];
    int idx;
    ...
    idx = (rel->addend / sizeof(void *));
    snprintf(pvname, sizeof(pvname), "pv_ops[%d]", idx);

There are only 7 chars for %d while it could take up to 9, so the printed "pv_ops[%d]" string could get truncated.

In reality the bug should never happen, because pv_ops only has ~80 entries, so 7 chars for the integer is more than enough. Still, it's worth fixing. Bump the buffer size by 2 bytes to silence the warning.

[ jpoimboe: changed size to 19; massaged changelog ]

Fixes db2b0c5d7b6f ("objtool: Support pv_opsindirect calls for noinstr")
---
 tools/objtool/check.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/objtool/check.c +1 -1
index 21735829b860..750ef1c446c8 100644
@@ -2823,7 +2823,7 @@ static inline bool func_uaccess_safe(struct symbol *func)
 
 static inline const char *call_dest_name(struct instruction *insn)
 {
-	static char pvname[16];
+	static char pvname[19];
 	struct reloc *rel;
 	int idx;
 
Signature · 1 line
2.37.0
#2 lore raw

PATCH 2/2 libsubcmd: Fix use-after-free for realloc(..., 0)

In reply to Tales Aparecida

From: Kees Cook <[email protected]>

GCC 12 correctly reports a potential use-after-free condition in the xrealloc helper. Fix the warning by avoiding an implicit "free(ptr)" when size == 0:

In file included from help.c:12:
In function 'xrealloc',
inlined from 'add_cmdname' at help.c:24:2: subcmd-util.h:56:23: error: pointer may be used after 'realloc' [-Werror=use-after-free]

   56 |                 ret = realloc(ptr, size);
      |                       ^~~~~~~~~~~~~~~~~~
subcmd-util.h:52:21: note: call to 'realloc' here
   52 |         void *ret = realloc(ptr, size);
      |                     ^~~~~~~~~~~~~~~~~~
subcmd-util.h:58:31: error: pointer may be used after 'realloc' [-Werror=use-after-free]
   58 |                         ret = realloc(ptr, 1);
      |                               ^~~~~~~~~~~~~~~
subcmd-util.h:52:21: note: call to 'realloc' here
   52 |         void *ret = realloc(ptr, size);
      |                     ^~~~~~~~~~~~~~~~~~
---
 tools/lib/subcmd/subcmd-util.h | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
tools/lib/subcmd/subcmd-util.h +2 -9
index 794a375dad36..b2aec04fce8f 100644
@@ -50,15 +50,8 @@ static NORETURN inline void die(const char *err, ...)
 static inline void *xrealloc(void *ptr, size_t size)
 {
 	void *ret = realloc(ptr, size);
-	if (!ret && !size)
-		ret = realloc(ptr, 1);
-	if (!ret) {
-		ret = realloc(ptr, size);
-		if (!ret && !size)
-			ret = realloc(ptr, 1);
-		if (!ret)
-			die("Out of memory, realloc failed");
-	}
+	if (!ret)
+		die("Out of memory, realloc failed");
 	return ret;
 }
 
Signature · 1 line
2.37.0