-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix aliasing-based undefined behavior in string functions.
Copies these functions verbatim from musl v1.2.0.
- Loading branch information
Showing
9 changed files
with
59 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
#include <string.h> | ||
#include <stdint.h> | ||
#include <limits.h> | ||
#include "libc.h" | ||
|
||
#define ALIGN (sizeof(size_t)-1) | ||
#define ALIGN (sizeof(size_t)) | ||
#define ONES ((size_t)-1/UCHAR_MAX) | ||
#define HIGHS (ONES * (UCHAR_MAX/2+1)) | ||
#define HASZERO(x) ((x)-ONES & ~(x) & HIGHS) | ||
|
||
char *__stpncpy(char *restrict d, const char *restrict s, size_t n) | ||
char *__stpcpy(char *restrict d, const char *restrict s) | ||
{ | ||
size_t *wd; | ||
const size_t *ws; | ||
|
||
if (((uintptr_t)s & ALIGN) == ((uintptr_t)d & ALIGN)) { | ||
for (; ((uintptr_t)s & ALIGN) && n && (*d=*s); n--, s++, d++); | ||
if (!n || !*s) goto tail; | ||
#ifdef __GNUC__ | ||
typedef size_t __attribute__((__may_alias__)) word; | ||
word *wd; | ||
const word *ws; | ||
if ((uintptr_t)s % ALIGN == (uintptr_t)d % ALIGN) { | ||
for (; (uintptr_t)s % ALIGN; s++, d++) | ||
if (!(*d=*s)) return d; | ||
wd=(void *)d; ws=(const void *)s; | ||
for (; n>=sizeof(size_t) && !HASZERO(*ws); | ||
n-=sizeof(size_t), ws++, wd++) *wd = *ws; | ||
for (; !HASZERO(*ws); *wd++ = *ws++); | ||
d=(void *)wd; s=(const void *)ws; | ||
} | ||
for (; n && (*d=*s); n--, s++, d++); | ||
tail: | ||
memset(d, 0, n); | ||
#endif | ||
for (; (*d=*s); s++, d++); | ||
|
||
return d; | ||
} | ||
|
||
weak_alias(__stpncpy, stpncpy); | ||
|
||
weak_alias(__stpcpy, stpcpy); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"a.html": 19929, | ||
"a.html": 19899, | ||
"a.html.gz": 8344, | ||
"total": 19929, | ||
"total": 19899, | ||
"total_gz": 8344 | ||
} |