diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2024-05-14 11:31:09 -0700 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2024-05-14 11:31:09 -0700 |
commit | 4b768bf062db22e042a731e4c385bb0b4fa21a0e (patch) | |
tree | 50c49aaaa16a068a42f9bdf0007897c6dac11ed0 /tools/include | |
parent | 590103732442b4bb83886f03f2ddd39d129c3289 (diff) | |
parent | 0adab2b6b7336fb6ee3c6456a432dad3b1d25647 (diff) |
Merge tag 'linux_kselftest-nolibc-6.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest
Pull nolibc updates from Shuah Khan:
- add support for uname(2)
- remove open-coded strnlen()
- export strlen()
- add tests for strlcat() and strlcpy()
- fix memory error in realloc()
- fix strlcat() and strlcpy() return code and size usage
* tag 'linux_kselftest-nolibc-6.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux-kselftest:
tools/nolibc: add support for uname(2)
tools/nolibc/string: remove open-coded strnlen()
selftests/nolibc: Add tests for strlcat() and strlcpy()
tools/nolibc: Fix strlcpy() return code and size usage
tools/nolibc: Fix strlcat() return code and size usage
tools/nolibc/string: export strlen()
tools/nolibc/stdlib: fix memory error in realloc()
Diffstat (limited to 'tools/include')
-rw-r--r-- | tools/include/nolibc/stdlib.h | 2 | ||||
-rw-r--r-- | tools/include/nolibc/string.h | 46 | ||||
-rw-r--r-- | tools/include/nolibc/sys.h | 27 |
3 files changed, 54 insertions, 21 deletions
diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index bacfd35c5156..5be9d3c7435a 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -185,7 +185,7 @@ void *realloc(void *old_ptr, size_t new_size) if (__builtin_expect(!ret, 0)) return NULL; - memcpy(ret, heap->user_p, heap->len); + memcpy(ret, heap->user_p, user_p_len); munmap(heap, heap->len); return ret; } diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index a01c69dd495f..f9ab28421e6d 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -123,7 +123,7 @@ char *strcpy(char *dst, const char *src) * thus itself, hence the asm() statement below that's meant to disable this * confusing practice. */ -static __attribute__((unused)) +__attribute__((weak,unused,section(".text.nolibc_strlen"))) size_t strlen(const char *str) { size_t len; @@ -187,22 +187,26 @@ char *strndup(const char *str, size_t maxlen) static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) { - size_t len; - char c; - - for (len = 0; dst[len]; len++) - ; - - for (;;) { - c = *src; - if (len < size) - dst[len] = c; - if (!c) + size_t len = strnlen(dst, size); + + /* + * We want len < size-1. But as size is unsigned and can wrap + * around, we use len + 1 instead. + */ + while (len + 1 < size) { + dst[len] = *src; + if (*src == '\0') break; len++; src++; } + if (len < size) + dst[len] = '\0'; + + while (*src++) + len++; + return len; } @@ -210,16 +214,18 @@ static __attribute__((unused)) size_t strlcpy(char *dst, const char *src, size_t size) { size_t len; - char c; - for (len = 0;;) { - c = src[len]; - if (len < size) - dst[len] = c; - if (!c) - break; - len++; + for (len = 0; len < size; len++) { + dst[len] = src[len]; + if (!dst[len]) + return len; } + if (size) + dst[size-1] = '\0'; + + while (src[len]) + len++; + return len; } diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h index dda9dffd1d74..7b82bc3cf107 100644 --- a/tools/include/nolibc/sys.h +++ b/tools/include/nolibc/sys.h @@ -22,6 +22,7 @@ #include <linux/stat.h> /* for statx() */ #include <linux/prctl.h> #include <linux/resource.h> +#include <linux/utsname.h> #include "arch.h" #include "errno.h" @@ -1140,6 +1141,32 @@ int umount2(const char *path, int flags) /* + * int uname(struct utsname *buf); + */ + +struct utsname { + char sysname[65]; + char nodename[65]; + char release[65]; + char version[65]; + char machine[65]; + char domainname[65]; +}; + +static __attribute__((unused)) +int sys_uname(struct utsname *buf) +{ + return my_syscall1(__NR_uname, buf); +} + +static __attribute__((unused)) +int uname(struct utsname *buf) +{ + return __sysret(sys_uname(buf)); +} + + +/* * int unlink(const char *path); */ |