Skip to content

Commit

Permalink
[libc] Give more functions restrict qualifiers (NFC) (#78061)
Browse files Browse the repository at this point in the history
strsep, strtok_r, strlcpy, and strlcat take restricted pointers as
parameters.
Add the restrict qualifiers to them.

Sources:
https://man7.org/linux/man-pages/man3/strsep.3.html
https://man7.org/linux/man-pages/man3/strtok_r.3.html
https://man.freebsd.org/cgi/man.cgi?strlcpy
  • Loading branch information
AtariDreams committed Jan 15, 2024
1 parent e2ce91f commit e06b5a2
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions clang/lib/Headers/llvm_libc_wrappers/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ char *strcpy(char *__restrict, const char *__restrict) __LIBC_ATTRS;
size_t strcspn(const char *, const char *) __LIBC_ATTRS;
char *strdup(const char *) __LIBC_ATTRS;
size_t strlen(const char *) __LIBC_ATTRS;
char *strncat(char *, const char *, size_t) __LIBC_ATTRS;
char *strncat(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
int strncmp(const char *, const char *, size_t) __LIBC_ATTRS;
char *strncpy(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
char *strndup(const char *, size_t) __LIBC_ATTRS;
size_t strnlen(const char *, size_t) __LIBC_ATTRS;
size_t strspn(const char *, const char *) __LIBC_ATTRS;
char *strtok(char *__restrict, const char *) __LIBC_ATTRS;
char *strtok(char *__restrict, const char *__restrict) __LIBC_ATTRS;
char *strtok_r(char *__restrict, const char *__restrict,
char **__restrict) __LIBC_ATTRS;
size_t strxfrm(char *__restrict, const char *__restrict, size_t) __LIBC_ATTRS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
typedef struct __sFILE {
unsigned char *_p;
} FILE;
FILE *fopen(const char * restrict, const char * restrict) __asm("_" "fopen" );
FILE *fopen(const char *restrict, const char *restrict) __asm("_" "fopen" );
int fputc(int, FILE *);
int fputs(const char * restrict, FILE * restrict) __asm("_" "fputs" );
int fputs(const char *restrict, FILE *restrict) __asm("_" "fputs" );
int fclose(FILE *);
void exit(int);

Expand Down
6 changes: 3 additions & 3 deletions clang/test/Analysis/Inputs/system-header-simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ int fflush(FILE *stream);
size_t strlen(const char *);

char *strcpy(char *restrict, const char *restrict);
char *strncpy(char *dst, const char *src, size_t n);
char *strsep(char **stringp, const char *delim);
void *memcpy(void *dst, const void *src, size_t n);
char *strncpy(char *restrict dst, const char *restrict src, size_t n);
char *strsep(char **restrict stringp, const char *restrict delim);
void *memcpy(void *restrict dst, const void *restrict src, size_t n);
void *memset(void *s, int c, size_t n);

typedef unsigned long __darwin_pthread_key_t;
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Analysis/bsd-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#define NULL ((void *)0)

typedef __typeof(sizeof(int)) size_t;
size_t strlcpy(char *dst, const char *src, size_t n);
size_t strlcat(char *dst, const char *src, size_t n);
size_t strlcpy(char *restrict dst, const char *restrict src, size_t n);
size_t strlcat(char *restrict dst, const char *restrict src, size_t n);
size_t strlen(const char *s);
void clang_analyzer_eval(int);

Expand Down
4 changes: 2 additions & 2 deletions clang/test/Analysis/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void clang_analyzer_eval(int);
int scanf(const char *restrict format, ...);
void *malloc(size_t);
void free(void *);
void *memcpy(void *dest, const void *src, size_t n);
void *memcpy(void *restrict dest, const void *restrict src, size_t n);

//===----------------------------------------------------------------------===
// strlen()
Expand Down Expand Up @@ -1252,7 +1252,7 @@ int strncasecmp_null_argument(char *a, size_t n) {
// strsep()
//===----------------------------------------------------------------------===

char *strsep(char **stringp, const char *delim);
char *strsep(char ** restrict stringp, const char * restrict delim);

void strsep_null_delim(char *s) {
strsep(&s, NULL); // expected-warning{{Null pointer passed as 2nd argument to strsep()}}
Expand Down
4 changes: 2 additions & 2 deletions libc/spec/bsd_ext.td
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ def BsdExtensions : StandardSpec<"BSDExtensions"> {
FunctionSpec<
"strlcat",
RetValSpec<SizeTType>,
[ArgSpec<CharPtr>, ArgSpec<ConstCharPtr>, ArgSpec<SizeTType>]
[ArgSpec<ConstCharRestrictedPtr>, ArgSpec<ConstCharRestrictedPtr>, ArgSpec<SizeTType>]
>,
FunctionSpec<
"strlcpy",
RetValSpec<SizeTType>,
[ArgSpec<CharPtr>, ArgSpec<ConstCharPtr>, ArgSpec<SizeTType>]
[ArgSpec<ConstCharRestrictedPtr>, ArgSpec<ConstCharRestrictedPtr>, ArgSpec<SizeTType>]
>,
FunctionSpec<
"strsep",
Expand Down
3 changes: 2 additions & 1 deletion libc/src/string/strsep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(char *, strsep, (char **stringp, const char *delim)) {
LLVM_LIBC_FUNCTION(char *, strsep,
(char **__restrict stringp, const char *__restrict delim)) {
if (!*stringp)
return nullptr;
return internal::string_token<false>(*stringp, delim, stringp);
Expand Down
2 changes: 1 addition & 1 deletion libc/src/string/strsep.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace LIBC_NAMESPACE {

char *strsep(char **stringp, const char *delim);
char *strsep(char **__restrict stringp, const char *__restrict delim);

} // namespace LIBC_NAMESPACE

Expand Down

0 comments on commit e06b5a2

Please sign in to comment.