From MAILER-DAEMON Mon Dec 23 04:23:53 2019 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1ijJwP-0005lV-3k for mharc-commit-inetutils@gnu.org; Mon, 23 Dec 2019 04:23:53 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51977) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ijJwL-0005lN-D2 for commit-inetutils@gnu.org; Mon, 23 Dec 2019 04:23:51 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ijJwI-0000rx-0y for commit-inetutils@gnu.org; Mon, 23 Dec 2019 04:23:47 -0500 Received: from mail5.windriver.com ([192.103.53.11]:45478 helo=mail5.wrs.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ijJwH-0000eJ-PK for commit-inetutils@gnu.org; Mon, 23 Dec 2019 04:23:45 -0500 Received: from ALA-HCA.corp.ad.wrs.com (ala-hca.corp.ad.wrs.com [147.11.189.40]) by mail5.wrs.com (8.15.2/8.15.2) with ESMTPS id xBN8IlAC004656 (version=TLSv1 cipher=AES256-SHA bits=256 verify=FAIL) for ; Mon, 23 Dec 2019 00:18:58 -0800 Received: from ala-lpggp2.wrs.com (147.11.105.123) by ALA-HCA.corp.ad.wrs.com (147.11.189.50) with Microsoft SMTP Server id 14.3.468.0; Mon, 23 Dec 2019 00:18:36 -0800 From: Zhixiong Chi To: Subject: [PATCH] inetutils: fix the rcp couldn't copy subdirectory issue Date: Mon, 23 Dec 2019 00:18:36 -0800 Message-ID: <20191223081836.127217-1-zhixiong.chi@windriver.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 192.103.53.11 X-BeenThere: commit-inetutils@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: CVS commit notices for the GNU Internet utilities List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 23 Dec 2019 09:23:51 -0000 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. eg: char tmp[20] = "test"; snprintf(tmp,20,"%s%s",tmp,"yes"); The result of tmp -> yes. It will cause the wrong value. The sink function flow is as follows: >sink(int argc, char*argv[]) >{ > ... > targ = *argv; > static char *namebuf = NULL; > free (namebuf); > namebuf = malloc (need); > snprintf (namebuf, cursize, "%s%s%s", targ, *targ ? "/" : "", cp); > np = namebuf; > vect[0] = np; > sink (1, vect); > ... >} At the same time, we couldn't add the condition(need > corsize), the same snprintf issue still exists. for example: #ls rccopy/*/* rccopy/fold1/1 rccopy/fold2/2 rccopy/fold3/3 Since the cursize static variable is still the old value after copying the rccopy/fold1/1, when it copys the directory rccopy/fold2 during recursive, the value of need < cursize, the namebuf still use the old vlaue without being allocated the new space, the incorrect namebuf value issue is still here. Signed-off-by: Zhixiong Chi --- src/rcp.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/rcp.c b/src/rcp.c index f1a12f04..29ebdb32 100644 --- a/src/rcp.c +++ b/src/rcp.c @@ -881,6 +881,7 @@ sink (int argc, char *argv[]) int setimes, targisdir, wrerrno; char ch, *cp, *np, *targ, *vect[1], buf[BUFSIZ]; const char *why; + static char *namebuf = NULL; #define atime tv[0] #define mtime tv[1] @@ -988,25 +989,14 @@ sink (int argc, char *argv[]) SCREWUP ("size not delimited"); if (targisdir) { - static char *namebuf = NULL; - static size_t cursize = 0; size_t need; need = strlen (targ) + strlen (cp) + 250; - if (need > cursize) + if (!(namebuf = malloc (need))); { - free (namebuf); - namebuf = malloc (need); - if (namebuf) - cursize = need; - else - { - run_err ("%s", strerror (errno)); - cursize = 0; - continue; - } + run_err ("%s", strerror (errno)); } - snprintf (namebuf, cursize, "%s%s%s", targ, *targ ? "/" : "", cp); + snprintf (namebuf, need, "%s%s%s", targ, *targ ? "/" : "", cp); np = namebuf; } else @@ -1163,6 +1153,8 @@ sink (int argc, char *argv[]) break; } } + if (namebuf) + free(namebuf); screwup: run_err ("protocol error: %s", why); exit (EXIT_FAILURE); -- 2.23.0