Skip to contentSkip to conversation

diff: "S_IFREG | 0644" to "(S_IFREG | 0644)" to avoid warning

PATCH In review Ævar Arnfjörð Bjarmason 6 participants 13 messages Jun 15, 2016 – Aug 13, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 diff: "S_IFREG | 0644" to "(S_IFREG | 0644)" to avoid warning

Wrap "S_IFREG | 0644" in parentheses to avoid a "suggest parentheses around arithmetic in operand of |" warning from GCC 4.1.3 on NetBSD 5.0.2.

I spotted and fixed this independently on NetBSD, but later found that there was a NetBSD Problem Report that included this fix.

NetBSD-PR: http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=42168

---
 builtin/diff.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
builtin/diff.c +1 -1
index a43d326..e8b7e09 100644
@@ -93,7 +93,7 @@ static int builtin_diff_blobs(struct rev_info *revs,
 			      int argc, const char **argv,
 			      struct blobinfo *blob)
 {
-	unsigned mode = canon_mode(S_IFREG | 0644);
+	unsigned mode = canon_mode((S_IFREG | 0644));
 
 	if (argc > 1)
 		usage(builtin_diff_usage);
Signature · 1 line
1.7.3.159.g610493
#1 lore raw

builtin/diff.c static int builtin_diff_blobs(struct rev_info *revs, · line 96

Quoted 2 lines · Ævar Arnfjörð Bjarmason
builtin/diff.c +1 -1
-	unsigned mode = canon_mode(S_IFREG | 0644);
+	unsigned mode = canon_mode((S_IFREG | 0644));

Just curious:

#define canon_mode(mode) \
(S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)

#define ce_permissions(mode) (((mode) & 0100) ? 0755 : 0644)

Since S_ISREG et al are macros, typically they would put their argument in parentheses in the definition. How are they defined in NetBSD sys/stat.h? What is canon_mode(S_IFREG | 0644) being misinterpreted to mean?

#2 lore raw
Quoted 11 lines · Jonathan Nieder
Quoted 2 lines · Ævar Arnfjörð Bjarmason

-     unsigned mode = canon_mode(S_IFREG | 0644);
+     unsigned mode = canon_mode((S_IFREG | 0644));

Just curious:

#define canon_mode(mode) \
       (S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
       S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)

Quoted 6 lines

#define ce_permissions(mode) (((mode) & 0100) ? 0755 : 0644)

Since S_ISREG et al are macros, typically they would put their argument in parentheses in the definition.  How are they defined in NetBSD sys/stat.h?  What is canon_mode(S_IFREG | 0644) being misinterpreted to mean?

Oh it's a bug in NetBSD, sorry for not being explicit about that:

    $ grep S_ISREG /usr/include/sys/stat.h
    #define S_ISREG(m)      ((m & _S_IFMT) == _S_IFREG)     /* regular file */
    $ grep S_ISREG /usr/include/linux/stat.h
    #define S_ISREG(m)      (((m) & S_IFMT) == S_IFREG)

I.e. GCC sees S_IFREG | 0644 & _S_IFMT' on NetBSD but (S_IFREG | 0644) & _S_IFMT' on Linux.

Since bitwise AND (&) has precedence over bitwise OR it's probably a logic error on NetBSD too, not just an annoying warning.

#3 lore raw
Quoted 5 lines · Ævar Arnfjörð Bjarmason

Oh it's a bug in NetBSD, sorry for not being explicit about that:

    $ grep S_ISREG /usr/include/sys/stat.h
    #define S_ISREG(m)      ((m & _S_IFMT) == _S_IFREG)     /* regular file */
Quoted 8 lines
    $ grep S_ISREG /usr/include/linux/stat.h
    #define S_ISREG(m)      (((m) & S_IFMT) == S_IFREG)

I.e. GCC sees S_IFREG | 0644 & _S_IFMT' on NetBSD but (S_IFREG | 0644) & _S_IFMT' on Linux.

Since bitwise AND (&) has precedence over bitwise OR it's probably a logic error on NetBSD too, not just an annoying warning.

In that case, I'd prefer to work this around at the definition of canon_mode(), like

    /*
     * extra ()-pair around S_ISREG() and friends to work around platform
     * header with buggy definitions like
     * #define S_ISREG(x) ((x & _S_IFMT) == _S_IFREG)
     */
    #define canon_mode(mode) \
    	(S_ISREG((mode)) ? (S_IFREG | ce_permissions(mode)) : \
        ...

instead of contaminating the calling sites. Otherwise new calling sites we will add in the future need to be aware of the same bug for no good reason.

#4 lore raw

PATCH v2 1/1 cache.h: work around broken NetBSD system headers

Wrap the mode' argument to the canon_mode' macro in extra parentheses to avoid a bug with the S_* macros in sys/stat.h on NetBSD.

This issue was originally spotted in NetBSD Problem Report #42168 and worked around by der Mouse, but later filed as a bug with NetBSD itself in NetBSD Problem Report #43937 by me.

The issue is that NetBSD doesn't take care to wrap its macro arguments in parentheses, so on Linux and other sane systems we have S_ISREG(m) defined as something like:

    (((m) & S_IFMT) == S_IFREG)

But on NetBSD:

    ((m & _S_IFMT) == _S_IFREG)

Since a caller in builtin/diff.c called our macro as `S_IFREG | 0644' this bug introduced a logic error on NetBSD, since the precedence of bit-wise & is higher than | in C.

NetBSD-PR: http://gnats.netbsd.org/cgi-bin/query-pr-single.pl?number=43937

---

Quoted 3 lines · Junio C Hamano

instead of contaminating the calling sites. Otherwise new calling sites we will add in the future need to be aware of the same bug for no good reason.

Agreed. Here's a v2 that does that. With an updated commit message to mention the bug I filed with NetBSD.

 cache.h |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)
cache.h +8 -2
index 2ef2fa3..891d5d0 100644
@@ -277,9 +277,15 @@ static inline int ce_to_dtype(const struct cache_entry *ce)
 	else
 		return DT_UNKNOWN;
 }
+
+/*
+ * We use extra parentheses around mode to work around a NetBSD issue
+ * described in NetBSD Problem Report #43937. See
+ * http://www.netbsd.org/cgi-bin/query-pr-single.pl?number=43937
+ */
 #define canon_mode(mode) \
-	(S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
-	S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)
+	(S_ISREG((mode)) ? (S_IFREG | ce_permissions(mode)) : \
+	S_ISLNK((mode)) ? S_IFLNK : S_ISDIR((mode)) ? S_IFDIR : S_IFGITLINK)
 
 #define flexible_size(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
 #define cache_entry_size(len) flexible_size(cache_entry,len)
Signature · 1 line
1.7.3.159.g610493
#5 lore raw
In reply to Ævar Arnfjörð Bjarmason

Am 04.10.2010 11:21, schrieb Ævar Arnfjörð Bjarmason:

Review

Quoted 2 lines
(quoted diff) +1 -1
-	unsigned mode = canon_mode(S_IFREG | 0644);
+	unsigned mode = canon_mode((S_IFREG | 0644));

That doesn't look pretty.

How about something like the following instead? It untangles the ?-:-chain in canon_mode and allows passing of an argument with side effects. All the S_ISxxx macros get a single variable as parameter. Does it fix the issue on NetBSD?

---
 cache.h |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
cache.h +10 -3
index 2ef2fa3..3d5ed51 100644
@@ -277,9 +277,16 @@ static inline int ce_to_dtype(const struct cache_entry *ce)
 	else
 		return DT_UNKNOWN;
 }
-#define canon_mode(mode) \
-	(S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
-	S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)
+static inline unsigned int canon_mode(unsigned int mode)
+{
+	if (S_ISREG(mode))
+		return S_IFREG | ce_permissions(mode);
+	if (S_ISLNK(mode))
+		return S_IFLNK;
+	if (S_ISDIR(mode))
+		return S_IFDIR;
+	return S_IFGITLINK;
+}
 
 #define flexible_size(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
 #define cache_entry_size(len) flexible_size(cache_entry,len)
Signature · 1 line
1.7.3
#6 lore raw
Quoted 13 lines · René Scharfe

How about something like the following instead?

cache.h +0 -3
index 2ef2fa3..3d5ed51 100644
@@ -277,9 +277,16 @@ static inline int ce_to_dtype(const struct cache_entry *ce)
 	else
 		return DT_UNKNOWN;
 }
-#define canon_mode(mode) \
-	(S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
-	S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)

cache.h static inline int ce_to_dtype(const struct cache_entry *ce) · line 289

Quoted 10 lines
cache.h +10 -0
+static inline unsigned int canon_mode(unsigned int mode)
+{
+	if (S_ISREG(mode))
+		return S_IFREG | ce_permissions(mode);
+	if (S_ISLNK(mode))
+		return S_IFLNK;
+	if (S_ISDIR(mode))
+		return S_IFDIR;
+	return S_IFGITLINK;
+}

That sounds much better to me. I don't know whether it fixes the issue on NetBSD though.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#8 lore raw

Am 04.10.2010 21:23, schrieb Junio C Hamano:

Quoted 2 lines

Looks much saner; thanks. I think we can steal the log message from Ævar v2 to explain what this change is about.

Good idea. :)

I forgot this:

#9 lore raw
In reply to Ævar Arnfjörð Bjarmason
Quoted 6 lines · Ævar Arnfjörð Bjarmason

Wrap "S_IFREG | 0644" in parentheses to avoid a "suggest parentheses around arithmetic in operand of |" warning from GCC 4.1.3 on NetBSD 5.0.2.

I spotted and fixed this independently on NetBSD, but later found that there was a NetBSD Problem Report that included this fix.

With this and Jonathan's xdiff patch git compiles without warnings on NetBSD, aside from this:

imap-send.c: In function 'ssl_socket_connect':
imap-send.c:310: warning: assignment discards qualifiers from
pointer target type
imap-send.c:312: warning: assignment discards qualifiers from
pointer target type

I don't see a sane way around that[1], since it appears the NetBSD people have patched openssl's function definitions without bumping the OpenSSL version number. Either that or OpenSSL itself changed from const char* to char* to const char* again, I didn't investigate that.

But tests on NetBSD with /bin/sh still fail since we use cd -P, but we have unapplied patches for that so I didn't pursue it:

    http://article.gmane.org/gmane.comp.version-control.git/136561/match=
    http://article.gmane.org/gmane.comp.version-control.git/136562/match=

1. We could check for __NetBSD__ and the NetBSD version, but it's not worthwhile for a single warning.

#10 lore raw
Quoted 5 lines · Ævar Arnfjörð Bjarmason

But tests on NetBSD with /bin/sh still fail since we use cd -P, but we have unapplied patches for that so I didn't pursue it:

    http://article.gmane.org/gmane.comp.version-control.git/136561/match=
    http://article.gmane.org/gmane.comp.version-control.git/136562/match=

Aren't these v1.7.0-rc0~76^2 and v1.7.0-rc0~76^2^?

Here's a patch for the more important of the remaining problems. I'm just guessing here; untested, of course.

-- 8< --
Subject: tests: use pwd -P to simulate cd -P for portability

NetBSD supports pwd -P but not cd -P. POSIX has required both for a while, so this should not be an issue for most Unix-like platforms.

The test harness uses cd -P to ensure $PWD and $(pwd) agree; cd $(pwd -P) should do that, too.

If pwd -P fails on some platform, with this patch, the test harness will die with 'FATAL: Unexpected exit with code 1'.

---
 t/test-lib.sh |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
t/test-lib.sh +4 -3
index 830e5e7..184bf84 100644
@@ -916,9 +916,10 @@ rm -fr "$test" || {
 }
 
 test_create_repo "$test"
-# Use -P to resolve symlinks in our working directory so that the cwd
-# in subprocesses like git equals our $PWD (for pathname comparisons).
-cd -P "$test" || exit 1
+cd "$test" || exit 1
+# Resolve symlinks in our working directory so that the cwd in
+# subprocesses like git equals our $PWD (for pathname comparisons).
+dir=$(pwd -P) && cd "$dir" || exit 1
 
 HOME=$(pwd)
 export HOME
Signature · 1 line
1.7.2.3
#11 lore raw
Quoted 1 line · Jonathan Nieder

The test harness uses cd -P to ensure $PWD and $(pwd) agree;

As Ævar noticed, this sentence as it stands doesn't make much sense. The idea is rather to make $(pwd -L) and $(pwd -P) agree --- the former is accessible through $PWD, the latter through getwd() and /bin/pwd.

#12 lore raw
In reply to Ævar Arnfjörð Bjarmason

Write good cheer, host.

--------------------------------------------------------
This
http://www.tomtop.com/black-remote-controller-charger2x-2800mah-battery-packs-for-wii_p11124.html?aid=z
Wii Charger looks good n stylish. It lights up all blue which made me think this is good. Get a
http://www.tomtop.com/mini-bluetooth-keyboard-for-ps3-mac-os-android-pc-pda.html?aid=z
Wireless Keyboard and mouse combo for your computer workstation and reduce desktop clutter.

-----
The voice input and output is very good in this
http://www.tomtop.com/black-ps3-wireless-bluetooth-headset-for-playstation-3.html?aid=z
Wireless PS3 Headset . It is compatible with all PS3 games.Buy from Reliable
http://www.tomtop.com/google-android-7-notebook-3g-tablet-pc-umpc-wifi-mid-pda.html?aid=z
Google Android PC apad Wholesalers.

Signature · 2 lines
View this message in context: http://git.661346.n2.nabble.com/PATCH-diff-S-IFREG-0644-to-S-IFREG-0644-to-avoid-warning-tp5598387p5598910.html
Sent from the git mailing list archive at Nabble.com.