From patchwork Wed Jun 19 10:03:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harmen Stoppels X-Patchwork-Id: 92424 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 706E238845F9 for ; Wed, 19 Jun 2024 10:04:18 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-40136.proton.ch (mail-40136.proton.ch [185.70.40.136]) by sourceware.org (Postfix) with ESMTPS id 44DAA388450F for ; Wed, 19 Jun 2024 10:03:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 44DAA388450F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=harmenstoppels.nl Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=harmenstoppels.nl ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 44DAA388450F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=185.70.40.136 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718791423; cv=none; b=tdJQ+f08YyqyvUPRwKLodWo0RbaYInYSBHrBWvvuH+OQwfmh2TAdjVwSB2gmwOrvy5OyscO61ftBbSIUzmk9PduU1Hp9Q18ITPtRaCoCxX39ia4sM+tcwMfvbqf/pm+0IuHV7TjO5xVUX80UvUXqPoi+FrO4ELa7Yu/ZvVUft+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718791423; c=relaxed/simple; bh=yaN59lIlrzDZdGboaiIaUH5wl4rWtl9+WiLt8ZySWKQ=; h=DKIM-Signature:Date:To:From:Subject:Message-ID:MIME-Version; b=GmQLC6W4Y073mMvZZkb62gjcBUoQTmBMl7ghkoGaer8yuNkj2vKEsPhZK3/VZxDW0L6wiTjzvEjb7pnYnpsVBzWHTmqmoTr9ElrSj1Wc5w44lUkwLJgmjyo4cKx2ffCjesLKe2B9M0YQ8PbFCskkfZu6EaYbEsB4/sRLhEGq8O0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=harmenstoppels.nl; s=protonmail; t=1718791419; x=1719050619; bh=/2fC7Nzjlatkbet40XNWBwyt2bwBH8pNy4k16cni+TQ=; h=Date:To:From:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=YT06yodD6jAvQfPVeCyLKeYXgXfwex3t2b5y2PBGRatU//PXx9BpEB/+ElA8U7d5C ufv0scYJLahNY5WbPCojftb0zrme/HYCRg+F0nepL1kmmeUEgVGe3M1il73xD4yakx KFOKHlA+LGPbGMFPwicjFYEzpYVzyfHC/vlQita8= Date: Wed, 19 Jun 2024 10:03:34 +0000 To: "binutils@sourceware.org" From: Harmen Stoppels Subject: [PATCH] libdep plugin: fix bugs in parser and improve escaping Message-ID: Feedback-ID: 4152449:user:proton X-Pm-Message-ID: d8ce02c45d02f8ae2b210fa37434630e5194c151 MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org Fix bugs in the __.LIBDEP string to argument list parser in the libdep plugin. PR ld/31906 * libdep_plugin.c (st2vec): fix bug where null byte was not copied on memmove, repeating the last character. Allow literal \ by escaping it with \\. Allow escaping of quotes inside quoted strings. Fix out of bounds errors leading to segfaults on input strings like `-L/'a\'b'`. Signed-off-by: Harmen Stoppels --- ld/libdep_plugin.c | 63 +++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 37 deletions(-) diff --git a/ld/libdep_plugin.c b/ld/libdep_plugin.c index 414f3cffe12..44fb7cbd588 100644 --- a/ld/libdep_plugin.c +++ b/ld/libdep_plugin.c @@ -138,7 +138,7 @@ str2vec (char *in) { char **res; char *s, *first, *end; - char *sq, *dq; + char *quote; int i; end = in + strlen (in); @@ -157,55 +157,44 @@ str2vec (char *in) return res; i = 0; - sq = NULL; - dq = NULL; + quote = NULL; res[0] = first; - for (s = first; *s; s++) + for (s = first; *s;) { if (*s == '\\') { - memmove (s, s+1, end-s-1); - end--; + /* trailing backslash is not an escape character */ + if (s + 1 == end) + break; + memmove (s, s + 1, end - s); + --end; + ++s; } - if (isspace ((unsigned char) *s)) + else if (*s == '\'' || *s == '\"') { - if (sq || dq) - continue; - *s++ = '\0'; - while (isspace ((unsigned char) *s)) s++; - if (*s) - res[++i] = s; - } - if (*s == '\'' && !dq) - { - if (sq) + if (!quote) + quote = s++; + else if (*s == *quote) { - memmove (sq, sq+1, s-sq-1); - memmove (s-2, s+1, end-s-1); + memmove (quote, quote + 1, s - quote - 1); + memmove (s - 1, s + 1, end - s); end -= 2; - s--; - sq = NULL; + --s; + quote = NULL; } else - { - sq = s; - } + ++s; } - if (*s == '"' && !sq) + else if (!quote && isspace ((unsigned char) *s)) { - if (dq) - { - memmove (dq, dq+1, s-dq-1); - memmove (s-2, s+1, end-s-1); - end -= 2; - s--; - dq = NULL; - } - else - { - dq = s; - } + *s++ = '\0'; + while (isspace ((unsigned char) *s)) + s++; + if (*s) + res[++i] = s; } + else + ++s; } res[++i] = NULL; return res;