Skip to content

Commit 9eca74d

Browse files
authored
Merge pull request #691 from danielinux/fix_strings_c
Fix strncasecmp bounds and alpha-only case folding
2 parents 0a738ac + 2244688 commit 9eca74d

4 files changed

Lines changed: 379 additions & 15 deletions

File tree

src/string.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ int strcasecmp(const char *s1, const char *s2)
124124
while (!diff && *s1) {
125125
diff = (int)*s1 - (int)*s2;
126126

127-
if ((diff == 'A' - 'a') || (diff == 'a' - 'A'))
127+
if (((diff == 'A' - 'a') || (diff == 'a' - 'A')) &&
128+
(isalpha((unsigned char)*s1) && isalpha((unsigned char)*s2)))
128129
diff = 0;
129130

130131
s1++;
@@ -142,12 +143,13 @@ int strncasecmp(const char *s1, const char *s2, size_t n)
142143
while (!diff && *s1) {
143144
diff = (int)*s1 - (int)*s2;
144145

145-
if ((diff == 'A' - 'a') || (diff == 'a' - 'A'))
146+
if (((diff == 'A' - 'a') || (diff == 'a' - 'A')) &&
147+
(isalpha((unsigned char)*s1) && isalpha((unsigned char)*s2)))
146148
diff = 0;
147149

148150
s1++;
149151
s2++;
150-
if (++i > n)
152+
if (++i >= n)
151153
break;
152154
}
153155
return diff;
@@ -156,16 +158,13 @@ int strncasecmp(const char *s1, const char *s2, size_t n)
156158
#if !defined(__CCRX__) /* Renesas CCRX */
157159
char *strncat(char *dest, const char *src, size_t n)
158160
{
159-
size_t i = 0;
161+
size_t i;
160162
size_t j = strlen(dest);
161163

162-
for (i = 0; i < strlen(src); i++) {
163-
if (j >= (n - 1)) {
164-
break;
165-
}
166-
dest[j++] = src[i];
164+
for (i = 0; i < n && src[i] != '\0'; i++) {
165+
dest[j + i] = src[i];
167166
}
168-
dest[j] = '\0';
167+
dest[j + i] = '\0';
169168

170169
return dest;
171170
}

tools/fdt-parser/fdt-parser.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,9 @@ int main(int argc, char *argv[])
498498
if (ret == 0) {
499499
char outfilename[PATH_MAX];
500500
strncpy(outfilename, filename, sizeof(outfilename)-1);
501-
strncat(outfilename, ".out", sizeof(outfilename)-1);
501+
outfilename[sizeof(outfilename) - 1] = '\0';
502+
strncat(outfilename, ".out",
503+
sizeof(outfilename) - strlen(outfilename) - 1);
502504

503505
/* save updated binary file */
504506
write_bin(outfilename, image, imageSz + UNIT_TEST_GROW_SIZE);

tools/unit-tests/Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ LDFLAGS+=-ftest-coverage
2525

2626

2727

28-
TESTS:=unit-parser unit-extflash unit-spi-flash unit-aes128 unit-aes256 \
29-
unit-chacha20 unit-pci unit-mock-state unit-sectorflags unit-image \
30-
unit-nvm unit-nvm-flagshome unit-enc-nvm unit-enc-nvm-flagshome \
31-
unit-delta unit-update-flash unit-update-ram unit-pkcs11_store
28+
TESTS:=unit-parser unit-extflash unit-string unit-spi-flash unit-aes128 \
29+
unit-aes256 unit-chacha20 unit-pci unit-mock-state unit-sectorflags \
30+
unit-image unit-nvm unit-nvm-flagshome unit-enc-nvm \
31+
unit-enc-nvm-flagshome unit-delta unit-update-flash unit-update-ram \
32+
unit-pkcs11_store
3233

3334
all: $(TESTS)
3435

@@ -94,6 +95,9 @@ unit-extflash: ../../include/target.h unit-extflash.c
9495
unit-spi-flash: ../../include/target.h unit-spi-flash.c
9596
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
9697

98+
unit-string: ../../include/target.h unit-string.c
99+
gcc -o $@ $^ $(CFLAGS) -DDEBUG_UART -DPRINTF_ENABLED $(LDFLAGS)
100+
97101
unit-aes128: ../../include/target.h unit-extflash.c
98102
gcc -o $@ $^ $(CFLAGS) $(LDFLAGS)
99103

0 commit comments

Comments
 (0)