From 2255ae94ea36d7855d705d9d6494e6e70f935a04 Mon Sep 17 00:00:00 2001 From: "J.W. Jagersma" Date: Wed, 28 Jun 2023 18:27:25 +0200 Subject: [PATCH 1/3] fix use-after-free warnings on gcc 12 --- src/libc/ansi/stdio/doscan.c | 9 +++++---- src/libc/ansi/stdio/rename.c | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/libc/ansi/stdio/doscan.c b/src/libc/ansi/stdio/doscan.c index b0d85ef7..b8a0d81b 100644 --- a/src/libc/ansi/stdio/doscan.c +++ b/src/libc/ansi/stdio/doscan.c @@ -533,16 +533,17 @@ _instr(char *ptr, int type, int len, FILE *iop, *ptr++ = '\0'; string_length++; } - if (allocate_char_buffer) + if (arg_ptr && allocate_char_buffer) { - *(char **)arg_ptr = realloc(orig_ptr, string_length); - ptr = arg_ptr; - if (!*ptr) + ptr = realloc(orig_ptr, string_length); + if (!ptr) { free(orig_ptr); errno = ENOMEM; return 0; } + + *(char **)arg_ptr = ptr; } return 1; diff --git a/src/libc/ansi/stdio/rename.c b/src/libc/ansi/stdio/rename.c index 742256d0..b955eda2 100644 --- a/src/libc/ansi/stdio/rename.c +++ b/src/libc/ansi/stdio/rename.c @@ -125,6 +125,7 @@ push_dir(const char *dir) /* Ensure we have enough space in the name pool for this directory. */ if (pool_end + dspace >= dirnames_pool + pool_size) { + const int old_ptr = (int) dirnames_pool; char * temp; /* Make its size doubled, plus a space for this directory. */ @@ -135,7 +136,7 @@ push_dir(const char *dir) errno = ENOMEM; return 0; } - pool_end += temp - dirnames_pool; + pool_end += (int) temp - old_ptr; dirnames_pool = temp; } @@ -143,6 +144,7 @@ push_dir(const char *dir) if (++stack_top - dirstack >= stack_size) { /* Not enough storage--reallocate. */ + const int old_ptr = (int) dirstack; Stacked_Dir * temp; stack_size *= 2; @@ -153,7 +155,7 @@ push_dir(const char *dir) errno = ENOMEM; return 0; } - stack_top += temp - dirstack; + stack_top += (int) temp - old_ptr; dirstack = temp; } -- 2.40.1