Skip to content

Commit f47d5a4

Browse files
committed
powerpc/prom_init: Convert prom_strcpy() into prom_strscpy_pad()
In a subsequent patch we'd like to have something like a strscpy_pad() implementation usable in prom_init.c. Currently we have a strcpy() implementation with only one caller, so convert it into strscpy_pad() and update the caller. Reviewed-by: Daniel Axtens <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 3018fbc commit f47d5a4

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

arch/powerpc/kernel/prom_init.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,31 @@ static int __init prom_strcmp(const char *cs, const char *ct)
242242
return 0;
243243
}
244244

245-
static char __init *prom_strcpy(char *dest, const char *src)
245+
static ssize_t __init prom_strscpy_pad(char *dest, const char *src, size_t n)
246246
{
247-
char *tmp = dest;
247+
ssize_t rc;
248+
size_t i;
248249

249-
while ((*dest++ = *src++) != '\0')
250-
/* nothing */;
251-
return tmp;
250+
if (n == 0 || n > INT_MAX)
251+
return -E2BIG;
252+
253+
// Copy up to n bytes
254+
for (i = 0; i < n && src[i] != '\0'; i++)
255+
dest[i] = src[i];
256+
257+
rc = i;
258+
259+
// If we copied all n then we have run out of space for the nul
260+
if (rc == n) {
261+
// Rewind by one character to ensure nul termination
262+
i--;
263+
rc = -E2BIG;
264+
}
265+
266+
for (; i < n; i++)
267+
dest[i] = '\0';
268+
269+
return rc;
252270
}
253271

254272
static int __init prom_strncmp(const char *cs, const char *ct, size_t count)
@@ -2701,7 +2719,7 @@ static void __init flatten_device_tree(void)
27012719

27022720
/* Add "phandle" in there, we'll need it */
27032721
namep = make_room(&mem_start, &mem_end, 16, 1);
2704-
prom_strcpy(namep, "phandle");
2722+
prom_strscpy_pad(namep, "phandle", sizeof("phandle"));
27052723
mem_start = (unsigned long)namep + prom_strlen(namep) + 1;
27062724

27072725
/* Build string array */

0 commit comments

Comments
 (0)