[PATCH] wget: fix --post-file with NUL bytes

Sertonix sertonix at posteo.net
Thu Jan 1 23:04:32 UTC 2026


function                                             old     new   delta
wget_main                                           3444    3490     +46
.rodata                                           100926  100930      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 50/0)               Total: 50 bytes

---
 networking/wget.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/networking/wget.c b/networking/wget.c
index ec3767793..8d2f9792c 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -252,6 +252,7 @@ struct globals {
 	char *dir_prefix;
 #if ENABLE_FEATURE_WGET_LONG_OPTIONS
 	char *post_data;
+	size_t post_size;
 	char *post_file;
 	char *extra_headers;
 	unsigned char user_headers; /* Headers mentioned by the user */
@@ -1262,8 +1263,11 @@ static void download_one_url(const char *url)
 
 		if (option_mask32 & WGET_OPT_POST_FILE) {
 			int fd = xopen_stdin(G.post_file);
-			G.post_data = xmalloc_read(fd, NULL);
+			G.post_size = INT_MAX - 4095;
+			G.post_data = xmalloc_read(fd, &G.post_size);
 			close(fd);
+		} else if (G.post_data) {
+			G.post_size = strlen(G.post_data);
 		}
 
 		if (G.post_data) {
@@ -1274,11 +1278,11 @@ static void download_one_url(const char *url)
 				);
 			}
 			SENDFMT(sfp,
-				"Content-Length: %u\r\n"
-				"\r\n"
-				"%s",
-				(int) strlen(G.post_data), G.post_data
+				"Content-Length: %zu\r\n"
+				"\r\n",
+				G.post_size
 			);
+			fwrite(G.post_data, 1, G.post_size, sfp);
 		} else
 #endif
 		{
-- 
2.52.0



More information about the busybox mailing list