Skip to content

Commit 610730f

Browse files
Fix Sign-Conversion warnings in library and test code (JuliaStrings#214)
* JuliaStrings#169 turn on sign-conversion warnings Signed-off-by: Mike Glorioso <mike.glorioso@gmail.com> * JuliaStrings#169 fix sign-conversion warnings for utf8proc.c fix sign-converstion warnings for utf8proc_iterate uc requires at most 21 bits to identify a unicode codepoint, so there is no need for it to be unsigned multiple locations use, modify, or store uc with a signed value the only exception is line 137 where uc is compared with an unsigned value fix sign-converstion warnings for utf8proc_tolower, utf8proc_toupper, utf8proc_totitle all three methods have sign conversion warnings when calling seqindex_decode_index seqindex_decode_index uses the passed value as an index to an array utf8proc_sequences as utf8proc_sequences is hard-coded and smaller than 2^31 - 1 we can safely cast to unsigned fix sign-converstion warnings for utf8proc_decompose_char lines with this warning use the defined function utf8proc_decompose_lump in the function, a hardcoded unsigned value (1<<12) is complemented then cast as a signed value as the intent is to remove the 12th bit flag from options, a signed value, and explicit cast is safe fix sign-conversion warnings for utf8proc_map_custom result is declared as signed, but is only expected to contain values between 0 and 4 sizeof returns an unsigned value. result must be cast to unsigned Signed-off-by: Mike Glorioso <mike.glorioso@gmail.com> * JuliaStrings#169 fix sign-conversion warnings for test/* fix sign-conversion warnings for test/tests.c encode change type for d to match return value of utf8proc_encode_char fix sign-conversion warnings for test/graphemetest.c checkline si, i, and j are unsigned size types, utf8proc_map and utf8proc_iterate accept and return signed size types utf8proc_map treats negative strlen values as 0. the strlen used by the test must be similarly limited utf8proc_iterate treats negative strlen values as 4 which will be less than the unsigned size fix unused-but-set-variable warning by checking the glen value fix sign-conversion warnings for test/case.c main the if block ensures that tested codepoint fits in wint_t, but needs to include u and l as well c, u, and l can be safely cast to wint_t fix sign-conversion warnings for test/iterate.c all values used for len are below 8, so an explicit cast is safe updated types for more portable test code fix sign-conversion warnings for test/printproperty.c main change type of c to signed to resolve all sign-converstion warnings. replace sscanf(... &c) wiht sscanf(... &x) followed by explicit sign converstion Signed-off-by: Mike Glorioso <mike.glorioso@gmail.com>
1 parent 0520d6f commit 610730f

7 files changed

Lines changed: 29 additions & 25 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ PERL=perl
1111
CFLAGS ?= -O2
1212
PICFLAG = -fPIC
1313
C99FLAG = -std=c99
14-
WCFLAGS = -Wall -Wextra -pedantic
14+
WCFLAGS = -Wsign-conversion -Wall -Wextra -pedantic
1515
UCFLAGS = $(CPPFLAGS) $(CFLAGS) $(PICFLAG) $(C99FLAG) $(WCFLAGS) -DUTF8PROC_EXPORTS $(UTF8PROC_DEFINES)
1616
LDFLAG_SHARED = -shared
1717
SOFLAG = -Wl,-soname

test/case.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ int main(int argc, char **argv)
2626
++error;
2727
}
2828

29-
if (sizeof(wint_t) > 2 || c < (1<<16)) {
30-
wint_t l0 = towlower(c), u0 = towupper(c);
29+
if (sizeof(wint_t) > 2 || (c < (1<<16) && u < (1<<16) && l < (1<<16))) {
30+
wint_t l0 = towlower((wint_t)c), u0 = towupper((wint_t)c);
3131

3232
/* OS unicode tables may be out of date. But if they
3333
do have a lower/uppercase mapping, hopefully it
3434
is correct? */
35-
if (l0 != c && l0 != l) {
35+
if (l0 != (wint_t)c && l0 != (wint_t)l) {
3636
fprintf(stderr, "MISMATCH %x != towlower(%x) == %x\n",
3737
l, c, l0);
3838
++error;
3939
}
40-
else if (l0 != l) { /* often true for out-of-date OS unicode */
40+
else if (l0 != (wint_t)l) { /* often true for out-of-date OS unicode */
4141
++better;
4242
/* printf("%x != towlower(%x) == %x\n", l, c, l0); */
4343
}
44-
if (u0 != c && u0 != u) {
44+
if (u0 != (wint_t)c && u0 != (wint_t)u) {
4545
fprintf(stderr, "MISMATCH %x != towupper(%x) == %x\n",
4646
u, c, u0);
4747
++error;
4848
}
49-
else if (u0 != u) { /* often true for out-of-date OS unicode */
49+
else if (u0 != (wint_t)u) { /* often true for out-of-date OS unicode */
5050
++better;
5151
/* printf("%x != towupper(%x) == %x\n", u, c, u0); */
5252
}

test/graphemetest.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void checkline(const char *_buf, bool verbose) {
4343
else
4444
i++;
4545
}
46-
glen = utf8proc_map(utf8, j, &g, UTF8PROC_CHARBOUND);
46+
glen = utf8proc_map(utf8, (utf8proc_ssize_t)j, &g, UTF8PROC_CHARBOUND);
4747
if (glen == UTF8PROC_ERROR_INVALIDUTF8) {
4848
/* the test file contains surrogate codepoints, which are only for UTF-16 */
4949
printf("line %zd: ignoring invalid UTF-8 codepoints\n", lineno);
@@ -66,7 +66,7 @@ void checkline(const char *_buf, bool verbose) {
6666
utf8proc_bool expectbreak = false;
6767
do {
6868
utf8proc_int32_t codepoint;
69-
i += utf8proc_iterate(src + i, si - i, &codepoint);
69+
i += (size_t)utf8proc_iterate(src + i, (utf8proc_ssize_t)(si - i), &codepoint);
7070
check(codepoint >= 0, "invalid UTF-8 data");
7171
if (codepoint == 0x002F)
7272
expectbreak = true;
@@ -110,6 +110,7 @@ int main(int argc, char **argv)
110110
utf8proc_uint8_t *g;
111111
glen = utf8proc_map(input, 6, &g, UTF8PROC_CHARBOUND);
112112
check(!strcmp((char*)g, (char*)output), "mishandled u+ffff and u+fffe grapheme breaks");
113+
check(glen != 6, "mishandled u+ffff and u+fffe grapheme breaks");
113114
free(g);
114115
};
115116

test/iterate.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ static int error;
88
#define CHECKVALID(pos, val, len) buf[pos] = val; testbytes(buf,len,len,__LINE__)
99
#define CHECKINVALID(pos, val, len) buf[pos] = val; testbytes(buf,len,UTF8PROC_ERROR_INVALIDUTF8,__LINE__)
1010

11-
static void testbytes(unsigned char *buf, int len, utf8proc_ssize_t retval, int line)
11+
static void testbytes(utf8proc_uint8_t *buf, utf8proc_ssize_t len, utf8proc_ssize_t retval, int line)
1212
{
1313
utf8proc_int32_t out[16];
1414
utf8proc_ssize_t ret;
1515

1616
/* Make a copy to ensure that memory is left uninitialized after "len"
1717
* bytes. This way, Valgrind can detect overreads.
1818
*/
19-
unsigned char tmp[16];
20-
memcpy(tmp, buf, len);
19+
utf8proc_uint8_t tmp[16];
20+
memcpy(tmp, buf, (unsigned long int)len);
2121

2222
tests++;
2323
if ((ret = utf8proc_iterate(tmp, len, out)) != retval) {
2424
fprintf(stderr, "Failed (%d):", line);
25-
for (int i = 0; i < len ; i++) {
25+
for (utf8proc_ssize_t i = 0; i < len ; i++) {
2626
fprintf(stderr, " 0x%02x", tmp[i]);
2727
}
2828
fprintf(stderr, " -> %zd\n", ret);
@@ -32,8 +32,8 @@ static void testbytes(unsigned char *buf, int len, utf8proc_ssize_t retval, int
3232

3333
int main(int argc, char **argv)
3434
{
35-
uint32_t byt;
36-
unsigned char buf[16];
35+
utf8proc_int32_t byt;
36+
utf8proc_uint8_t buf[16];
3737

3838
(void) argc; (void) argv; /* unused */
3939

test/printproperty.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ int main(int argc, char **argv)
88

99
for (i = 1; i < argc; ++i) {
1010
utf8proc_uint8_t cstr[16], *map;
11-
unsigned int c;
11+
utf8proc_uint32_t x;
12+
utf8proc_int32_t c;
1213
if (!strcmp(argv[i], "-V")) {
1314
printf("utf8proc version %s\n", utf8proc_version());
1415
continue;
1516
}
16-
check(sscanf(argv[i],"%x",&c) == 1, "invalid hex input %s", argv[i]);
17+
check(sscanf(argv[i],"%x", &x) == 1, "invalid hex input %s", argv[i]);
18+
c = (utf8proc_int32_t)x;
1719
const utf8proc_property_t *p = utf8proc_get_property(c);
1820

1921
if (utf8proc_codepoint_valid(c))

test/tests.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ size_t skipspaces(const unsigned char *buf, size_t i)
2929
in dest, returning the number of bytes read from buf */
3030
size_t encode(unsigned char *dest, const unsigned char *buf)
3131
{
32-
size_t i = 0, j, d = 0;
32+
size_t i = 0, j;
33+
utf8proc_ssize_t d = 0;
3334
for (;;) {
3435
int c;
3536
i = skipspaces(buf, i);

utf8proc.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ UTF8PROC_DLLEXPORT const char *utf8proc_errmsg(utf8proc_ssize_t errcode) {
125125
UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(
126126
const utf8proc_uint8_t *str, utf8proc_ssize_t strlen, utf8proc_int32_t *dst
127127
) {
128-
utf8proc_uint32_t uc;
128+
utf8proc_int32_t uc;
129129
const utf8proc_uint8_t *end;
130130

131131
*dst = -1;
@@ -137,7 +137,7 @@ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_iterate(
137137
return 1;
138138
}
139139
// Must be between 0xc2 and 0xf4 inclusive to be valid
140-
if ((uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
140+
if ((utf8proc_uint32_t)(uc - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
141141
if (uc < 0xe0) { // 2-byte sequence
142142
// Must have valid continuation character
143143
if (str >= end || !utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
@@ -376,19 +376,19 @@ static utf8proc_ssize_t seqindex_write_char_decomposed(utf8proc_uint16_t seqinde
376376
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_tolower(utf8proc_int32_t c)
377377
{
378378
utf8proc_int32_t cl = utf8proc_get_property(c)->lowercase_seqindex;
379-
return cl != UINT16_MAX ? seqindex_decode_index(cl) : c;
379+
return cl != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cl) : c;
380380
}
381381

382382
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_toupper(utf8proc_int32_t c)
383383
{
384384
utf8proc_int32_t cu = utf8proc_get_property(c)->uppercase_seqindex;
385-
return cu != UINT16_MAX ? seqindex_decode_index(cu) : c;
385+
return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c;
386386
}
387387

388388
UTF8PROC_DLLEXPORT utf8proc_int32_t utf8proc_totitle(utf8proc_int32_t c)
389389
{
390390
utf8proc_int32_t cu = utf8proc_get_property(c)->titlecase_seqindex;
391-
return cu != UINT16_MAX ? seqindex_decode_index(cu) : c;
391+
return cu != UINT16_MAX ? seqindex_decode_index((utf8proc_uint32_t)cu) : c;
392392
}
393393

394394
UTF8PROC_DLLEXPORT int utf8proc_islower(utf8proc_int32_t c)
@@ -420,7 +420,7 @@ UTF8PROC_DLLEXPORT const char *utf8proc_category_string(utf8proc_int32_t c) {
420420

421421
#define utf8proc_decompose_lump(replacement_uc) \
422422
return utf8proc_decompose_char((replacement_uc), dst, bufsize, \
423-
options & ~UTF8PROC_LUMP, last_boundclass)
423+
options & ~(unsigned int)UTF8PROC_LUMP, last_boundclass)
424424

425425
UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_decompose_char(utf8proc_int32_t uc, utf8proc_int32_t *dst, utf8proc_ssize_t bufsize, utf8proc_option_t options, int *last_boundclass) {
426426
const utf8proc_property_t *property;
@@ -735,7 +735,7 @@ UTF8PROC_DLLEXPORT utf8proc_ssize_t utf8proc_map_custom(
735735
*dstptr = NULL;
736736
result = utf8proc_decompose_custom(str, strlen, NULL, 0, options, custom_func, custom_data);
737737
if (result < 0) return result;
738-
buffer = (utf8proc_int32_t *) malloc(result * sizeof(utf8proc_int32_t) + 1);
738+
buffer = (utf8proc_int32_t *) malloc(((utf8proc_size_t)result) * sizeof(utf8proc_int32_t) + 1);
739739
if (!buffer) return UTF8PROC_ERROR_NOMEM;
740740
result = utf8proc_decompose_custom(str, strlen, buffer, result, options, custom_func, custom_data);
741741
if (result < 0) {

0 commit comments

Comments
 (0)