aboutsummaryrefslogtreecommitdiffstats
path: root/main/busybox/0001-echo-do-not-assume-that-free-leaves-errno-unmodified.patch
blob: ab445baedf846bbdcdbb0c87ad26c0ab6ba0633c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
From ffd2a7e631ddf25ee7097fc64b45283f7f8910d3 Mon Sep 17 00:00:00 2001
From: Natanael Copa <ncopa@alpinelinux.org>
Date: Thu, 21 Jan 2021 14:12:51 +0100
Subject: [PATCH] echo: do not assume that free() leaves errno unmodified

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 | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/coreutils/echo.c b/coreutils/echo.c
index b3828894c..002832ead 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);
-	free(buffer);
-	if (/*WRONG:r < 0*/ errno) {
+	err = full_write(STDOUT_FILENO, buffer, out - buffer) != out - buffer;
+	if (err) {
 		bb_simple_perror_msg(bb_msg_write_error);
-		return 1;
 	}
-	return 0;
+	free(buffer);
+	return err;
 }
 
 /*
-- 
2.30.0