From patchwork Mon Oct 19 06:09:54 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yann Sionneau X-Patchwork-Id: 1384008 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=uclibc-ng.org (client-ip=89.238.66.15; helo=helium.openadk.org; envelope-from=devel-bounces@uclibc-ng.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=sionneau.net Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; secure) header.d=sionneau.net header.i=@sionneau.net header.a=rsa-sha1 header.s=selectormx3 header.b=aw9GeRhP; dkim-atps=neutral Received: from helium.openadk.org (helium.openadk.org [89.238.66.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4CF5vs2g7xz9sSC for ; Mon, 19 Oct 2020 17:10:13 +1100 (AEDT) Received: from helium.openadk.org (localhost [IPv6:::1]) by helium.openadk.org (Postfix) with ESMTP id B3DE210166; Mon, 19 Oct 2020 08:10:04 +0200 (CEST) X-Original-To: devel@uclibc-ng.org Delivered-To: devel@helium.openadk.org Received: from mx3.sionneau.net (mx3.sionneau.net [163.172.183.177]) by helium.openadk.org (Postfix) with ESMTPS id B411710166 for ; Mon, 19 Oct 2020 08:10:00 +0200 (CEST) Received: from mx3.sionneau.net (localhost [127.0.0.1]) by mx3.sionneau.net. (OpenSMTPD) with ESMTP id d451ba97 for ; Mon, 19 Oct 2020 06:09:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha1; c=simple; d=sionneau.net; h=from:to:cc :subject:date:message-id:mime-version:content-transfer-encoding; s=selectormx3; bh=sgRvF7BBd6RTXg8C7LKL5EO9scQ=; b=aw9GeRhPamKTA CEkIKXaxxKMgPQezwlDiZ6qljtAY18vueDuppD48gjg6bGeeDhD60Jg4KwwGuILK QILATU+o8NxnKPBDLBfKOPo7GDgxIiskLIEjO62Ld55nTFLxYHemgFU3F4uBWRuH SqSEcmg66lrsQ5lQeeLjjbwMETv/dg= DomainKey-Signature: a=rsa-sha1; c=simple; d=sionneau.net; h=from:to:cc :subject:date:message-id:mime-version:content-transfer-encoding; q=dns; s=selectormx3; b=Hk+5ghTWRN9RtTgUxFTVlYqB+gl/y89tzSmtDMf I/Mh6/r06r4Ofl1EVblaM+LJjQRgn8ufda7d0oYNAc4QV/tdxTQEBtGcp4FgWG3D lC2EZYWHQomwGjOri31+A001Do3s8j3I3TzEXsarXY4pSQlm5tlDmhhS/k+kkCPJ dYSg= Received: by mx3.sionneau.net (OpenSMTPD) with ESMTPSA id b56bd384 (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128:NO); Mon, 19 Oct 2020 06:09:59 +0000 (UTC) From: yann@sionneau.net To: devel@uclibc-ng.org Date: Mon, 19 Oct 2020 08:09:54 +0200 Message-Id: <20201019060954.1377364-1-yann@sionneau.net> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Subject: [uclibc-ng-devel] [PATCH] hasmntopt: better option matching X-BeenThere: devel@uclibc-ng.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: uClibc-ng Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: devel-bounces@uclibc-ng.org Sender: "devel" From: Yann Sionneau Previous implementation was respecting the man page description of what the function should do. Also the function does not seem to be defined by POSIX. But... to be really useful the function needs to handle option matching and not just substring matching. This is copy pasted from glibc. This fixes issue reported by https://github.com/wbx-github/uclibc-ng/issues/8 that can happen for instance there: https://github.com/frida/glib/blob/master/gio/gunixmounts.c#L622 Signed-off-by: Yann Sionneau --- libc/misc/mntent/mntent.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/libc/misc/mntent/mntent.c b/libc/misc/mntent/mntent.c index 608f84c1a..610e35af6 100644 --- a/libc/misc/mntent/mntent.c +++ b/libc/misc/mntent/mntent.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2000-2006 Erik Andersen + * Copyright (C) 1995-2020 Free Software Foundation, Inc. * * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. */ @@ -92,9 +93,25 @@ int addmntent(FILE * filep, const struct mntent *mnt) mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 0 ? 1 : 0); } -char *hasmntopt(const struct mntent *mnt, const char *opt) +/* Search MNT->mnt_opts for an option matching OPT. + Returns the address of the substring, or null if none found. */ +char *hasmntopt (const struct mntent *mnt, const char *opt) { - return strstr(mnt->mnt_opts, opt); + const size_t optlen = strlen(opt); + char *rest = mnt->mnt_opts, *p; + + while ((p = strstr(rest, opt)) != NULL) { + if ((p == rest || p[-1] == ',') + && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ',')) + return p; + + rest = strchr(p, ','); + if (rest == NULL) + break; + ++rest; + } + + return NULL; } FILE *setmntent(const char *name, const char *mode)