[PATCH] iconv: iconv.c: fix null pointer dereference when outbuf is NULL

Anton Moryakov ant.v.moryakov@gmail.com
Wed Oct 29 14:16:03 GMT 2025


When calling iconv() with outbuf == NULL (a valid use case for state
reset), the original code could dereference a null pointer in the
computation of (*outbuf + *outbytesleft), leading to a segmentation fault.

This patch adds null checks for 'outbuf' before dereferencing it or
using *outbuf in pointer arithmetic, passing NULL to __gconv instead.

The fix ensures safe handling of the legitimate case where the output
buffer is omitted, aligning with POSIX and GNU semantics for conversion
state management.

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
 iconv/iconv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/iconv/iconv.c b/iconv/iconv.c
index 7e082cae43..8f5865ba2b 100644
--- a/iconv/iconv.c
+++ b/iconv/iconv.c
@@ -50,8 +50,8 @@ iconv (iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf,
 
       result = __gconv (gcd, (const unsigned char **) inbuf,
 			(const unsigned char *)  (*inbuf + *inbytesleft),
-			(unsigned char **) outbuf,
-			(unsigned char *) (*outbuf + *outbytesleft),
+			outbuf ? (unsigned char **) outbuf : NULL,
+			outbuf ? (unsigned char *) (*outbuf + *outbytesleft) : NULL,
 			&irreversible);
 
       *inbytesleft -= *inbuf - instart;
-- 
2.39.2



More information about the Libc-alpha mailing list