Uh oh!
There was an error while loading. Please reload this page.
fix(strvec_{posix,str}cmp): use unsigned char to be consistent with strcmp - #1
Open
akinomyoga wants to merge 1 commit into
Open
fix(strvec_{posix,str}cmp): use unsigned char to be consistent with strcmp#1akinomyoga wants to merge 1 commit into
akinomyoga wants to merge 1 commit into
Conversation
…onsistent with strcmp
The present code tries to reduce the call to "strcmp" by comparing the
first character of the strings inline, but the comparison is made by
char (which is signed in a typical environment). This is inconsistent
with strcmp, which compares strings using unsigned char.
I'm not sure if I need to show an actual affected case, but it is hard
to create a natural case. I'm not sure if this can be robustly
reproducible in other systems, but the following demonstrates the
issue in my system (Linux, with strcoll(3) from glibc).
$ mkdir t
$ cd t
$ LANG=C
$ touch {,a}{$'\1',$'\xA9'}.txt
$ LANG=en_US.UTF-8
$ printf '%s\n' *.txt | cat -v
a^A.txt
aM-).txt
M-).txt
^A.txt
\x01 and \xA9 are shown by ^A and M-) in the "cat -v" output. Here,
we can observe that, when \x01 and \xA9 are the second characters of
the filenames, \x01 comes first. However, when they are the first
characters of the filenames, \xA9 comes first because it is a negative
number (-87) in char.akinomyogaforce-pushed
the
patch-strvec_posixcmp
branch
from
August 13, 2026 21:52
3496d08 to
10642adCompareakinomyoga
commented
Aug 13, 2026
MemberAuthor
Another possibility is to prepare a function for the common part: staticintstrvec_truestrcmp (char**s1, char**s2)
{
intresult;
if ((result= (unsigned char)**s1- (unsigned char)**s2) ==0)
result=strcmp (*s1, *s2);
returnresult;
}and make |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The present code tries to reduce the call to
strcmpby comparing the first character of the strings inline, but the comparison is made bychar(which is signed in a typical environment). This is inconsistent withstrcmp, which compares strings usingunsigned char.I'm not sure if I need to show an actual affected case, but it is hard to create a natural case. I'm not sure if this can be robustly reproducible in other systems, but the following demonstrates the issue in my system (Linux, with
strcoll(3)from glibc).\x01and\xA9are shown by^AandM-)in thecat -voutput. Here, we can observe that, when\x01and\xA9are the second characters of the filenames,\x01comes first. However, when they are the first characters of the filenames,\xA9comes first because it is a negative number (-87) inchar.