Skip to content

Commit b90d9b8

Browse files
René Scharfegitster
authored andcommitted
work around buggy S_ISxxx(m) implementations
There are buggy implementations of S_ISxxx(m) macros on some platforms (e.g. NetBSD). The issue is that NetBSD doesn't take care to wrap its macro arguments in parentheses, so on Linux and 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. [jc: took change description from Ævar Arnfjörð Bjarmason's patch] Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 349362c commit b90d9b8

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cache.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,16 @@ static inline int ce_to_dtype(const struct cache_entry *ce)
277277
else
278278
return DT_UNKNOWN;
279279
}
280-
#define canon_mode(mode) \
281-
(S_ISREG(mode) ? (S_IFREG | ce_permissions(mode)) : \
282-
S_ISLNK(mode) ? S_IFLNK : S_ISDIR(mode) ? S_IFDIR : S_IFGITLINK)
280+
static inline unsigned int canon_mode(unsigned int mode)
281+
{
282+
if (S_ISREG(mode))
283+
return S_IFREG | ce_permissions(mode);
284+
if (S_ISLNK(mode))
285+
return S_IFLNK;
286+
if (S_ISDIR(mode))
287+
return S_IFDIR;
288+
return S_IFGITLINK;
289+
}
283290

284291
#define flexible_size(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)
285292
#define cache_entry_size(len) flexible_size(cache_entry,len)

0 commit comments

Comments
 (0)