[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] echo: do not assume that free() leaves errno unmodified
From:       Natanael Copa <ncopa () alpinelinux ! org>
Date:       2021-01-21 13:38:31
Message-ID: 20210121133831.29636-1-ncopa () alpinelinux ! org
[Download RAW message or body]

musl libc's mallocng free() may modify errno if kernel does not support
MADV_FREE which causes echo to echo with error when it shouldn't.

Future versions of POSIX[1] will require that free() leaves errno
unmodified but til then, do not rely free() implementation.

Should fix downstream issues:
https://github.com/alpinelinux/docker-alpine/issues/134
https://gitlab.alpinelinux.org/alpine/aports/-/issues/12311

Bloatcheck on x86_64:

function                                             old     new   delta
echo_main                                            414     406      -8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-8)               Total: -8
bytes
   text	   data	    bss	    dec	    hex	filename
    881114	  15196	   2000	 898310	  db506	busybox_old
     881106	  15196	   2000	 898302	  db4fe	busybox_unstripped
---
 coreutils/echo.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/coreutils/echo.c b/coreutils/echo.c
index b3828894c..9e8939a00 100644
--- a/coreutils/echo.c
+++ b/coreutils/echo.c
@@ -97,6 +97,7 @@ int echo_main(int argc UNUSED_PARAM, char **argv)
 #else
 	char nflag = 1;
 	char eflag = 0;
+	int err;
 
 	while ((arg = *++argv) != NULL) {
 		char n, e;
@@ -184,14 +185,12 @@ int echo_main(int argc UNUSED_PARAM, char **argv)
 
  do_write:
 	/* Careful to error out on partial writes too (think ENOSPC!) */
-	errno = 0;
-	/*r =*/ full_write(STDOUT_FILENO, buffer, out - buffer);
+	err = full_write(STDOUT_FILENO, buffer, out - buffer) != out - buffer;
 	free(buffer);
-	if (/*WRONG:r < 0*/ errno) {
+	if (err) {
 		bb_simple_perror_msg(bb_msg_write_error);
-		return 1;
 	}
-	return 0;
+	return err;
 }
 
 /*
-- 
2.30.0

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic