[LEDE-DEV,procd,02/17] utrace: Fix off-by-one errors

Message ID 20170912111250.31576-5-sojkam1@fel.cvut.cz
State Accepted
Headers show
Series
  • [LEDE-DEV,procd,01/17] utrace: Fix environment initialization
Related show

Commit Message

Michal Sojka Sept. 12, 2017, 11:12 a.m.
This fixes two errors:

1) memcpy() copies envc elements starting from index 1, so the number
   of elements in target array should be envc + 1. But only envc was
   allocated.

2) If original environment envp is empty, i.e. it contains only a NULL
   element, the while loop misses it.

Signed-off-by: Michal Sojka <sojkam1@fel.cvut.cz>
---
 trace/trace.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Patch

diff --git a/trace/trace.c b/trace/trace.c
index 04bf7a5..65fe067 100644
--- a/trace/trace.c
+++ b/trace/trace.c
@@ -177,7 +177,7 @@  int main(int argc, char **argv, char **envp)
 		char **_argv = calloc(argc + 1, sizeof(char *));
 		char **_envp;
 		char *preload = "LD_PRELOAD=/lib/libpreload-trace.so";
-		int envc = 1;
+		int envc = 0;
 		int ret;
 
 		memcpy(_argv, argv, argc * sizeof(char *));
@@ -185,7 +185,7 @@  int main(int argc, char **argv, char **envp)
 		while (envp[envc++])
 			;
 
-		_envp = calloc(envc, sizeof(char *));
+		_envp = calloc(envc + 1, sizeof(char *));
 		memcpy(&_envp[1], envp, envc * sizeof(char *));
 		*_envp = preload;