summaryrefslogtreecommitdiff
path: root/lib/sanitizer_common/sanitizer_libc.cc
diff options
context:
space:
mode:
authorAlexey Samsonov <samsonov@google.com>2012-06-15 13:09:52 +0000
committerAlexey Samsonov <samsonov@google.com>2012-06-15 13:09:52 +0000
commitc925697df6626bb0ea27ea96539bf0580f8f3d3d (patch)
tree83430d737636d136acbc1bc4765e162336dbc5ef /lib/sanitizer_common/sanitizer_libc.cc
parent88207ab15125e2f1e9b3d541b735b2b8aba9b6d9 (diff)
[Sanitizer] move all the rest re-implementations of libc functions from ASan runtime to common sanitizer runtime
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@158519 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/sanitizer_common/sanitizer_libc.cc')
-rw-r--r--lib/sanitizer_common/sanitizer_libc.cc61
1 files changed, 60 insertions, 1 deletions
diff --git a/lib/sanitizer_common/sanitizer_libc.cc b/lib/sanitizer_common/sanitizer_libc.cc
index 898ca1931..62b9bf7ae 100644
--- a/lib/sanitizer_common/sanitizer_libc.cc
+++ b/lib/sanitizer_common/sanitizer_libc.cc
@@ -15,7 +15,8 @@
namespace __sanitizer {
-void MiniLibcStub() {
+s64 internal_atoll(const char *nptr) {
+ return internal_simple_strtoll(nptr, (char**)0, 10);
}
void *internal_memchr(const void *s, int c, uptr n) {
@@ -99,6 +100,15 @@ uptr internal_strlen(const char *s) {
return i;
}
+char *internal_strncat(char *dst, const char *src, uptr n) {
+ uptr len = internal_strlen(dst);
+ uptr i;
+ for (i = 0; i < n && src[i]; i++)
+ dst[len + i] = src[i];
+ dst[len + i] = 0;
+ return dst;
+}
+
char *internal_strncpy(char *dst, const char *src, uptr n) {
uptr i;
for (i = 0; i < n && src[i]; i++)
@@ -108,4 +118,53 @@ char *internal_strncpy(char *dst, const char *src, uptr n) {
return dst;
}
+uptr internal_strnlen(const char *s, uptr maxlen) {
+ uptr i = 0;
+ while (i < maxlen && s[i]) i++;
+ return i;
+}
+
+char *internal_strstr(const char *haystack, const char *needle) {
+ // This is O(N^2), but we are not using it in hot places.
+ uptr len1 = internal_strlen(haystack);
+ uptr len2 = internal_strlen(needle);
+ if (len1 < len2) return 0;
+ for (uptr pos = 0; pos <= len1 - len2; pos++) {
+ if (internal_memcmp(haystack + pos, needle, len2) == 0)
+ return (char*)haystack + pos;
+ }
+ return 0;
+}
+
+s64 internal_simple_strtoll(const char *nptr, char **endptr, int base) {
+ CHECK(base == 10);
+ while (IsSpace(*nptr)) nptr++;
+ int sgn = 1;
+ u64 res = 0;
+ bool have_digits = false;
+ char *old_nptr = (char*)nptr;
+ if (*nptr == '+') {
+ sgn = 1;
+ nptr++;
+ } else if (*nptr == '-') {
+ sgn = -1;
+ nptr++;
+ }
+ while (IsDigit(*nptr)) {
+ res = (res <= UINT64_MAX / 10) ? res * 10 : UINT64_MAX;
+ int digit = ((*nptr) - '0');
+ res = (res <= UINT64_MAX - digit) ? res + digit : UINT64_MAX;
+ have_digits = true;
+ nptr++;
+ }
+ if (endptr != 0) {
+ *endptr = (have_digits) ? (char*)nptr : old_nptr;
+ }
+ if (sgn > 0) {
+ return (s64)(Min((u64)INT64_MAX, res));
+ } else {
+ return (res > INT64_MAX) ? INT64_MIN : ((s64)res * -1);
+ }
+}
+
} // namespace __sanitizer