Skip to content

Commit 6a5762d

Browse files
committed
Addressed copilot review comments
- Converted 'strncat' to the standard POSIX behavior. Fixed fdt-parser to use the right len argument.
1 parent 25ae592 commit 6a5762d

3 files changed

Lines changed: 14 additions & 20 deletions

File tree

src/string.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@ int strcmp(const char *s1, const char *s2)
117117
}
118118
#endif /* Renesas CCRX */
119119

120-
static int is_alpha(int c)
121-
{
122-
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
123-
}
124-
125120
int strcasecmp(const char *s1, const char *s2)
126121
{
127122
int diff = 0;
@@ -130,7 +125,7 @@ int strcasecmp(const char *s1, const char *s2)
130125
diff = (int)*s1 - (int)*s2;
131126

132127
if (((diff == 'A' - 'a') || (diff == 'a' - 'A')) &&
133-
(is_alpha((unsigned char)*s1) || is_alpha((unsigned char)*s2)))
128+
(isalpha((unsigned char)*s1) && isalpha((unsigned char)*s2)))
134129
diff = 0;
135130

136131
s1++;
@@ -149,7 +144,7 @@ int strncasecmp(const char *s1, const char *s2, size_t n)
149144
diff = (int)*s1 - (int)*s2;
150145

151146
if (((diff == 'A' - 'a') || (diff == 'a' - 'A')) &&
152-
(is_alpha((unsigned char)*s1) || is_alpha((unsigned char)*s2)))
147+
(isalpha((unsigned char)*s1) && isalpha((unsigned char)*s2)))
153148
diff = 0;
154149

155150
s1++;
@@ -163,16 +158,13 @@ int strncasecmp(const char *s1, const char *s2, size_t n)
163158
#if !defined(__CCRX__) /* Renesas CCRX */
164159
char *strncat(char *dest, const char *src, size_t n)
165160
{
166-
size_t i = 0;
161+
size_t i;
167162
size_t j = strlen(dest);
168163

169-
for (i = 0; i < strlen(src); i++) {
170-
if (j >= (n - 1)) {
171-
break;
172-
}
173-
dest[j++] = src[i];
164+
for (i = 0; i < n && src[i] != '\0'; i++) {
165+
dest[j + i] = src[i];
174166
}
175-
dest[j] = '\0';
167+
dest[j + i] = '\0';
176168

177169
return dest;
178170
}

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/unit-string.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424

2525
#define FAST_MEMCPY
26-
#define DEBUG_UART
27-
#define PRINTF_ENABLED
2826

2927
#include <check.h>
3028
#include <stdint.h>
@@ -86,7 +84,9 @@ START_TEST(test_case_insensitive_alpha_only)
8684
ck_assert_int_ne(strcasecmp("@", "`"), 0);
8785
ck_assert_int_ne(strcasecmp("[", "{"), 0);
8886
ck_assert_int_ne(strcasecmp("]", "}"), 0);
87+
ck_assert_int_ne(strcasecmp("!", "A"), 0);
8988
ck_assert_int_ne(strncasecmp("@", "`", 1), 0);
89+
ck_assert_int_ne(strncasecmp("!", "A", 1), 0);
9090
ck_assert_int_ne(strcasecmp("a@", "A`"), 0);
9191
ck_assert_int_ne(strncasecmp("a@", "A`", 2), 0);
9292
}
@@ -171,11 +171,11 @@ START_TEST(test_strcpy_strncpy_strcat_strncat)
171171

172172
strcpy(dest, "a");
173173
strncat(dest, "bc", 3);
174-
ck_assert_str_eq(dest, "ab");
174+
ck_assert_str_eq(dest, "abc");
175175

176176
strcpy(dest, "a");
177177
strncat(dest, "bc", 1);
178-
ck_assert_str_eq(dest, "a");
178+
ck_assert_str_eq(dest, "ab");
179179

180180
strcpy(dest, "");
181181
strncat(dest, "x", 2);
@@ -289,7 +289,7 @@ START_TEST(test_uart_printf_formats)
289289
ck_assert_str_eq(uart_buf, "0012");
290290

291291
reset_uart_buf();
292-
uart_printf("%p", (void*)0x10);
292+
uart_printf("%p", (void*)(uintptr_t)0x10);
293293
ck_assert_str_eq(uart_buf, "0x10");
294294

295295
reset_uart_buf();

0 commit comments

Comments
 (0)