* [PATCH v2] symlinks: use unsigned int for flags
@ 2026-01-21 16:26 Tian Yuchen
2026-01-21 18:04 ` Junio C Hamano
0 siblings, 1 reply; 3+ messages in thread
From: Tian Yuchen @ 2026-01-21 16:26 UTC (permalink / raw)
To: git; +Cc: ps
The 'flags' and 'track_flags' fields in symlinks.c are used
strictly as a collection of bits (using bitwise operators including
&, |, ~). Using a signed integer for bitmasks may lead to undefined
behavior with shift operations and logic errors if the MSB is touched.
Change these fields from 'int' to 'unsigned int' to align with C
standards and typical usage patterns.
Signed-off-by: Tian Yuchen <a3205153416@gmail.com>
---
Changes in v2:
Decouple definition of 'ret' and 'saved_errno' from 'save_flags'.
'ret' captures the return value of lstat() which can be -1, so it
must remain signed. Same applies to 'saved_errno'.
(Thanks to Patrick Steinhardt for spotting this)
---
symlinks.c | 13 +++++++------
symlinks.h | 4 ++--
2 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/symlinks.c b/symlinks.c
index 9cc090d42c..9e01ab3bc8 100644
--- a/symlinks.c
+++ b/symlinks.c
@@ -74,11 +74,12 @@ static inline void reset_lstat_cache(struct cache_def *cache)
*/
static int lstat_cache_matchlen(struct cache_def *cache,
const char *name, int len,
- int *ret_flags, int track_flags,
+ unsigned int *ret_flags, unsigned int track_flags,
int prefix_len_stat_func)
{
int match_len, last_slash, last_slash_dir, previous_slash;
- int save_flags, ret, saved_errno = 0;
+ unsigned int save_flags;
+ int ret, saved_errno = 0;
struct stat st;
if (cache->track_flags != track_flags ||
@@ -192,10 +193,10 @@ static int lstat_cache_matchlen(struct cache_def *cache,
return match_len;
}
-static int lstat_cache(struct cache_def *cache, const char *name, int len,
- int track_flags, int prefix_len_stat_func)
+static unsigned int lstat_cache(struct cache_def *cache, const char *name, int len,
+ unsigned int track_flags, int prefix_len_stat_func)
{
- int flags;
+ unsigned int flags;
(void)lstat_cache_matchlen(cache, name, len, &flags, track_flags,
prefix_len_stat_func);
return flags;
@@ -234,7 +235,7 @@ int check_leading_path(const char *name, int len, int warn_on_lstat_err)
static int threaded_check_leading_path(struct cache_def *cache, const char *name,
int len, int warn_on_lstat_err)
{
- int flags;
+ unsigned int flags;
int match_len = lstat_cache_matchlen(cache, name, len, &flags,
FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
int saved_errno = errno;
diff --git a/symlinks.h b/symlinks.h
index 7ae3d5b856..25bf04f54f 100644
--- a/symlinks.h
+++ b/symlinks.h
@@ -5,8 +5,8 @@
struct cache_def {
struct strbuf path;
- int flags;
- int track_flags;
+ unsigned int flags;
+ unsigned int track_flags;
int prefix_len_stat_func;
};
#define CACHE_DEF_INIT { \
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] symlinks: use unsigned int for flags
2026-01-21 16:26 [PATCH v2] symlinks: use unsigned int for flags Tian Yuchen
@ 2026-01-21 18:04 ` Junio C Hamano
2026-02-16 17:20 ` [PATCH v3 1/1] " Tian Yuchen
0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2026-01-21 18:04 UTC (permalink / raw)
To: Tian Yuchen; +Cc: git, ps
Tian Yuchen <a3205153416@gmail.com> writes:
> The 'flags' and 'track_flags' fields in symlinks.c are used
> strictly as a collection of bits (using bitwise operators including
> &, |, ~). Using a signed integer for bitmasks may lead to undefined
> behavior with shift operations and logic errors if the MSB is touched.
Which we do not do, so the "signed can lead to bugs" is a valid
concern and moving to unsigned is a good mitigation, but ...
>
> Change these fields from 'int' to 'unsigned int' to align with C
> standards and typical usage patterns.
... I'd tone it down a bit by replacing "aling with C standards and
typical" with "match our", if I were writing this.
>
> Signed-off-by: Tian Yuchen <a3205153416@gmail.com>
>
> ---
> Changes in v2:
>
> Decouple definition of 'ret' and 'saved_errno' from 'save_flags'.
> 'ret' captures the return value of lstat() which can be -1, so it
> must remain signed. Same applies to 'saved_errno'.
>
> (Thanks to Patrick Steinhardt for spotting this)
Yes, indeed. Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v3 1/1] symlinks: use unsigned int for flags
2026-01-21 18:04 ` Junio C Hamano
@ 2026-02-16 17:20 ` Tian Yuchen
0 siblings, 0 replies; 3+ messages in thread
From: Tian Yuchen @ 2026-02-16 17:20 UTC (permalink / raw)
To: git; +Cc: gitster
The 'flags' and 'track_flags' fields in symlinks.c are used
strictly as a collection of bits (using bitwise operators including
&, |, ~). Using a signed integer for bitmasks may lead to undefined
behavior with shift operations and logic errors if the MSB is touched.
Change these fields from 'int' to 'unsigned int' to match our usage
patterns.
Signed-off-by: Tian Yuchen <a3205153416@gmail.com>
---
Changes in v3:
The commit message.
---
symlinks.c | 13 +++++++------
symlinks.h | 4 ++--
2 files changed, 9 insertions(+), 8 deletions(-)
diff --git a/symlinks.c b/symlinks.c
index 9cc090d42c..9e01ab3bc8 100644
--- a/symlinks.c
+++ b/symlinks.c
@@ -74,11 +74,12 @@ static inline void reset_lstat_cache(struct cache_def *cache)
*/
static int lstat_cache_matchlen(struct cache_def *cache,
const char *name, int len,
- int *ret_flags, int track_flags,
+ unsigned int *ret_flags, unsigned int track_flags,
int prefix_len_stat_func)
{
int match_len, last_slash, last_slash_dir, previous_slash;
- int save_flags, ret, saved_errno = 0;
+ unsigned int save_flags;
+ int ret, saved_errno = 0;
struct stat st;
if (cache->track_flags != track_flags ||
@@ -192,10 +193,10 @@ static int lstat_cache_matchlen(struct cache_def *cache,
return match_len;
}
-static int lstat_cache(struct cache_def *cache, const char *name, int len,
- int track_flags, int prefix_len_stat_func)
+static unsigned int lstat_cache(struct cache_def *cache, const char *name, int len,
+ unsigned int track_flags, int prefix_len_stat_func)
{
- int flags;
+ unsigned int flags;
(void)lstat_cache_matchlen(cache, name, len, &flags, track_flags,
prefix_len_stat_func);
return flags;
@@ -234,7 +235,7 @@ int check_leading_path(const char *name, int len, int warn_on_lstat_err)
static int threaded_check_leading_path(struct cache_def *cache, const char *name,
int len, int warn_on_lstat_err)
{
- int flags;
+ unsigned int flags;
int match_len = lstat_cache_matchlen(cache, name, len, &flags,
FL_SYMLINK|FL_NOENT|FL_DIR, USE_ONLY_LSTAT);
int saved_errno = errno;
diff --git a/symlinks.h b/symlinks.h
index 7ae3d5b856..25bf04f54f 100644
--- a/symlinks.h
+++ b/symlinks.h
@@ -5,8 +5,8 @@
struct cache_def {
struct strbuf path;
- int flags;
- int track_flags;
+ unsigned int flags;
+ unsigned int track_flags;
int prefix_len_stat_func;
};
#define CACHE_DEF_INIT { \
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-02-16 17:20 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-21 16:26 [PATCH v2] symlinks: use unsigned int for flags Tian Yuchen
2026-01-21 18:04 ` Junio C Hamano
2026-02-16 17:20 ` [PATCH v3 1/1] " Tian Yuchen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox