Skip to content

Commit

Permalink
lib/string: Fix strcpy and strcpy_isr implementation
Browse files Browse the repository at this point in the history
Current version of the strncpy and strncpy_isr pads with 0 remaining
n-1 bytes after copying src string so strcpy shouldn't use strncpy
with len = SIZE_MAX because this will cause buffer overflow.

Signed-off-by: Oleksii Moisieiev <[email protected]>
Reviewed-by: Michalis Pappas <[email protected]>
Approved-by: Simon Kuenzer <[email protected]>
GitHub-Closes: unikraft#1269
  • Loading branch information
oleksiimoisieiev authored and razvand committed Jan 23, 2024
1 parent 4769155 commit 4567716
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/isrlib/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ char *strncpy_isr(char *dst, const char *src, size_t len)

char *strcpy_isr(char *dst, const char *src)
{
return strncpy_isr(dst, src, SIZE_MAX);
char *save = dst;

for (; (*dst = *src) != '\0'; ++src, ++dst)
;
return save;
}

int strncmp_isr(const char *str1, const char *str2, size_t len)
Expand Down
6 changes: 5 additions & 1 deletion lib/nolibc/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ char *strncpy(char *dst, const char *src, size_t len)

char *strcpy(char *dst, const char *src)
{
return strncpy(dst, src, SIZE_MAX);
char *save = dst;

for (; (*dst = *src) != '\0'; ++src, ++dst)
;
return save;
}

int strncmp(const char *str1, const char *str2, size_t len)
Expand Down

0 comments on commit 4567716

Please sign in to comment.