From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10327 Path: news.gmane.org!not-for-mail From: =?UTF-8?Q?Daniel_Cegie=C5=82ka?= Newsgroups: gmane.linux.lib.musl.general Subject: dirname() / basename() - musl vs FreeBSD and OpenBSD Date: Sun, 24 Jul 2016 21:12:49 +0200 Message-ID: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1469387625 26261 80.91.229.3 (24 Jul 2016 19:13:45 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 24 Jul 2016 19:13:45 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-10340-gllmg-musl=m.gmane.org@lists.openwall.com Sun Jul 24 21:13:31 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1bROq8-0003y1-Uw for gllmg-musl@m.gmane.org; Sun, 24 Jul 2016 21:13:29 +0200 Original-Received: (qmail 7473 invoked by uid 550); 24 Jul 2016 19:13:26 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 7406 invoked from network); 24 Jul 2016 19:13:20 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=2buvqEHaj+0Ni3d1J1DJlYnV4n4+s2YpVd6H/TI52pE=; b=z1ZGTOR1lejmETB2CAPagKzs1+lxqgm3Hzk3Cvs6U7HGrDu5Ps8lnyaeetg6YFJAfM niD5ZscqIOMt/l2lbsSHVpZw+KyTw3BClX+IKWAzB9SfLF5bF8h7dZtXm2Bf6rixBDZd pJKl6Bj5PsEdMdUpcrqlW+uMx/N6bFAJjHGFAJvJgxB6bq0JEupmaCL0npgs/3o1/dmZ 6cUVJ9LPKV+yeXHaEbw4gMyLy/41fMNcpVTjdizBQCLDRA5wIiN2S4ZW5s7EY9p9J5VP t4orfRwb+Xe5w+rxQs4Q3MILXVeBwzzATV/KmbexILo1dbrIfxLOCW5ZtFZE1WtNONzN UOhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=2buvqEHaj+0Ni3d1J1DJlYnV4n4+s2YpVd6H/TI52pE=; b=DJxCDNWXNaoMztYMkujCcqlLgXo6YLBXpLZMISkbPfdjZ94TPzNhCgmsIkfYf5CfpC pRsUmGb9yC/P+evT4ndaOz88lcLEiZI8gDCSvDzf6T8S12v+S1oFp6AFDUc827nwjYCj bvILJeHnwLjpbaf+vG4Ul3DB+dliWg3mFqUQZmMmssI0mgV6OvE9Jkyb8gHgYISr/KHq B4SM+b/FOiZozt64E7g4okSlmBSwboPL6nxZxgqgLzkVjVY4H4zs2ZD3scCWzSNU0KB8 mPO6gQ3NV0Gx2Yict2G4V59NadG7mXLqUM4Uq7/fvTnTTEvHZKkT7dq8z3zR10Q06BQW tiXQ== X-Gm-Message-State: AEkoous32xfniYnmuyWpyJ1kIyijFx/Fcxg+PB0NtArw6vIbqDeVYLZui11767J7wyna+0vl3RZj/SnMMdF41Q== X-Received: by 10.31.185.9 with SMTP id j9mr6859572vkf.144.1469387588412; Sun, 24 Jul 2016 12:13:08 -0700 (PDT) Xref: news.gmane.org gmane.linux.lib.musl.general:10327 Archived-At: Hi, I came across a very strange problem when I ports code from OpenBSD to musl-libc, and it seems, that a lot of problems can be caused by dirname(). http://pubs.opengroup.org/onlinepubs/009695399/functions/dirname.html "The dirname() function >>> may <<< modify the string pointed to by path, and may return a pointer to static storage that may then be overwritten by subsequent calls to dirname()." OpenBSD and FreeBSD: http://man.openbsd.org/OpenBSD-5.8/dirname.3 https://www.freebsd.org/cgi/man.cgi?query=dirname&sektion=3 "dirname() returns a pointer to internal static storage space that will be overwritten by subsequent calls (each function has its own separate storage). Other vendor implementations of dirname() may modify the contents of the string passed to dirname(); this should be taken into account when writing code which calls this function if portability is desired." NetBSD: http://netbsd.gw.com/cgi-bin/man-cgi?dirname+3+NetBSD-7.0 "BUGS (...) The dirname() function returns a pointer to static storage that may be overwritten by subse- quent calls to dirname(). This is not strictly a bug; it is explicitly allowed by IEEE Std 1003.1-2001 (``POSIX.1'')." so: #include /* musl libc dirname() */ #include int main() { char s1[] = "/usr/lib/"; char s2[] = "/usr/lib/"; char *p1, *p2; p1 = dirname(s1); p2 = openbsd_dirname(s2); printf("musl: s1: %s, p1: %s\n", s1, p1); printf("openbsd_dirname: s2: %s, p2: %s\n", s2, p2); return 0; } # ./a.out musl: s1: /usr, p1: /usr openbsd_dirname: s2: /usr/lib/, p2: /usr So if you use the code from OpenBSD or FreeBSD, then you should be very careful... grep, sed, patch, diff... etc. everything is potentially error prone. musl has very good support for code from *BSD, so is the ability that dirname() in musl does not overwrite argument of the function? It will not change anything in relation to the IEEE Std 1003.1-2001, but it will be much safer for the code from FreeBSD and OpenBSD. btw. the same problem applies to basename(): http://netbsd.gw.com/cgi-bin/man-cgi?basename+3+NetBSD-7.0 http://man.openbsd.org/OpenBSD-5.8/man3/basename.3 https://www.freebsd.org/cgi/man.cgi?query=basename&apropos=0&sektion=3&manpath=FreeBSD+10.3-RELEASE+and+Ports&arch=default&format=html Daniel