[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[bug-inetutils] [PULL-REQUEST][PATCH] src/rcp.c: fix couldn't copy subdi
[bug-inetutils] [PULL-REQUEST][PATCH] src/rcp.c: fix couldn't copy subdirectory issue
Thu, 30 May 2019 03:00:09 -0700
The namebuf will get the same allocation address as the one before
free operation, at the same time because of the recursive call for
sink function, the targ and namebuf point the same address, then it
cause the namebuf will get the wrong value with the snprintf function.
Since the snprintf function doesn't like the strcpy function which
can overwrite the destination.
char tmp = "test";
The result of tmp -> yes other than "testyes". It will cause the wrong value.
So we couldn't free at this place before allocation(since recursive call).
The sink function flow is as follows:
>sink(int argc, char*argv)
> targ = *argv;
> if (targisdir)
> static char *namebuf = NULL;
> free (namebuf);
> namebuf = malloc (need);
> snprintf (namebuf, cursize, "%s%s%s", targ, *targ ? "/" : "", cp); -> issue
> np = namebuf;
> if (buf == "D")
> vect = np;
> sink (1, vect);
I move the free operation into the end of every loop to avoid the memory leak,
due to vect = np = namebuf, the np always used until the end of loop.
At the same time because of the scope of namebuf, we free np instead of namebuf
in the loop to avoid the compiling failure if the targisdir is true.
># cd /root
># rcp address@hidden:/test/test1 ./
># ls ./test
Result: Only the test directory had been copied. All of the subdirectory
hadn't been copied. At the same time since the snprintf error test1
will be copyied into / directory.
Signed-off-by: Zhixiong Chi <address@hidden>
src/rcp.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/rcp.c b/src/rcp.c
index f1a12f04..9357eae5 100644
@@ -995,7 +995,6 @@ sink (int argc, char *argv)
need = strlen (targ) + strlen (cp) + 250;
if (need > cursize)
- free (namebuf);
namebuf = malloc (need);
cursize = need;
@@ -1162,6 +1161,8 @@ sink (int argc, char *argv)
+ if (targisdir && np)
run_err ("protocol error: %s", why);
- [bug-inetutils] [PULL-REQUEST][PATCH] src/rcp.c: fix couldn't copy subdirectory issue,
Zhixiong Chi <=