Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vstring: Avoid int -> char truncation warnings #3690

Merged
merged 5 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions main/vstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,7 @@ extern void vStringCopyToLower (vString *const dest, const vString *const src)
vStringResize (dest, src->size);
d = dest->buffer;
for (i = 0 ; i < length ; ++i)
{
int c = s [i];

d [i] = tolower (c);
}
d [i] = (char) tolower (s [i]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your change itself looks good to me.

Following the consideration that I made during reviewing is not directly related to this pull request. If I wrote incorrect things, please, let me know.

s [i] can be a negative if char on this platform means signed char.
In tolower(3) man page:

    If c is neither an unsigned char value nor EOF, the behavior of these functions is undefined. 

So tolower (s [i]) can be a trouble. Am I wrong?

If we declare s as unsigned char *, and d as unsigned char *,
we can avoid the trouble.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

8fffa20 cares what I feared here.

d [i] = '\0';
}

Expand Down Expand Up @@ -294,7 +290,7 @@ extern char *vStringStrdup (const vString *const string)
return str;
}

static char valueToXDigit (int v)
static int valueToXDigit (int v)
{
Assert (v >= 0 && v <= 0xF);

Expand Down
2 changes: 1 addition & 1 deletion main/vstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ CTAGS_INLINE void vStringPut (vString *const string, const int c)
if (string->length + 1 == string->size) /* check for buffer overflow */
vStringResize (string, string->size * 2);

string->buffer [string->length] = c;
string->buffer [string->length] = (char) c;
b4n marked this conversation as resolved.
Show resolved Hide resolved
if (c != '\0')
string->buffer [++string->length] = '\0';
}
Expand Down