From e0555f5bdf389f667ebae19f07e7b3d338fca20b Mon Sep 17 00:00:00 2001 From: "digit@chromium.org" Date: Fri, 3 Aug 2012 15:01:52 +0000 Subject: SORRY. It seems I've screwed up my commit for http://breakpad.appspot.com/411002/, since revision 1001 only contains the new src/client/linux/linux_libc_support.cc and none of the other required files. I'm not sure what happened, but I'm re-uploading the patch has another issue. Review URL: https://breakpad.appspot.com/426002 git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@1002 4c0a9323-5329-0410-9bdc-e9ce6186880e --- src/common/linux/linux_libc_support.h | 141 +++++------------------- src/common/linux/linux_libc_support_unittest.cc | 38 +++++++ 2 files changed, 67 insertions(+), 112 deletions(-) (limited to 'src/common/linux') diff --git a/src/common/linux/linux_libc_support.h b/src/common/linux/linux_libc_support.h index 2ef2dc11..b2f47af8 100644 --- a/src/common/linux/linux_libc_support.h +++ b/src/common/linux/linux_libc_support.h @@ -40,83 +40,21 @@ extern "C" { -static inline size_t -my_strlen(const char* s) { - size_t len = 0; - while (*s++) len++; - return len; -} - -static inline int -my_strcmp(const char* a, const char* b) { - for (;;) { - if (*a < *b) - return -1; - else if (*a > *b) - return 1; - else if (*a == 0) - return 0; - a++; - b++; - } -} - -static inline int -my_strncmp(const char* a, const char* b, size_t len) { - for (size_t i = 0; i < len; ++i) { - if (*a < *b) - return -1; - else if (*a > *b) - return 1; - else if (*a == 0) - return 0; - a++; - b++; - } - - return 0; -} +extern size_t my_strlen(const char* s); + +extern int my_strcmp(const char* a, const char* b); + +extern int my_strncmp(const char* a, const char* b, size_t len); // Parse a non-negative integer. // result: (output) the resulting non-negative integer // s: a NUL terminated string // Return true iff successful. -static inline bool -my_strtoui(int* result, const char* s) { - if (*s == 0) - return false; - int r = 0; - for (;; s++) { - if (*s == 0) - break; - const int old_r = r; - r *= 10; - if (*s < '0' || *s > '9') - return false; - r += *s - '0'; - if (r < old_r) - return false; - } - - *result = r; - return true; -} +extern bool my_strtoui(int* result, const char* s); // Return the length of the given, non-negative integer when expressed in base // 10. -static inline unsigned -my_int_len(intmax_t i) { - if (!i) - return 1; - - int len = 0; - while (i) { - len++; - i /= 10; - } - - return len; -} +extern unsigned my_int_len(intmax_t i); // Convert a non-negative integer to a string // output: (output) the resulting string is written here. This buffer must be @@ -124,54 +62,33 @@ my_int_len(intmax_t i) { // required length. // i: the non-negative integer to serialise. // i_len: the length of the integer in base 10 (see |my_int_len|). -static inline void -my_itos(char* output, intmax_t i, unsigned i_len) { - for (unsigned index = i_len; index; --index, i /= 10) - output[index - 1] = '0' + (i % 10); -} - -static inline const char* -my_strchr(const char* haystack, char needle) { - while (*haystack && *haystack != needle) - haystack++; - if (*haystack == needle) - return haystack; - return (const char*) 0; -} +extern void my_itos(char* output, intmax_t i, unsigned i_len); + +extern const char* my_strchr(const char* haystack, char needle); + +extern const char* my_strrchr(const char* haystack, char needle); // Read a hex value // result: (output) the resulting value // s: a string // Returns a pointer to the first invalid charactor. -static inline const char* -my_read_hex_ptr(uintptr_t* result, const char* s) { - uintptr_t r = 0; - - for (;; ++s) { - if (*s >= '0' && *s <= '9') { - r <<= 4; - r += *s - '0'; - } else if (*s >= 'a' && *s <= 'f') { - r <<= 4; - r += (*s - 'a') + 10; - } else if (*s >= 'A' && *s <= 'F') { - r <<= 4; - r += (*s - 'A') + 10; - } else { - break; - } - } - - *result = r; - return s; -} - -static inline void -my_memset(void* ip, char c, size_t len) { - char* p = (char *) ip; - while (len--) - *p++ = c; -} +extern const char* my_read_hex_ptr(uintptr_t* result, const char* s); + +extern const char* my_read_decimal_ptr(uintptr_t* result, const char* s); + +extern void my_memset(void* ip, char c, size_t len); + +// The following are considered safe to use in a compromised environment. +// Besides, this gives the compiler an opportunity to optimize their calls. +#define my_memcpy memcpy +#define my_memmove memmove +#define my_memcmp memcmp + +extern size_t my_strlcpy(char* s1, const char* s2, size_t len); + +extern size_t my_strlcat(char* s1, const char* s2, size_t len); + +extern int my_isspace(int ch); } // extern "C" diff --git a/src/common/linux/linux_libc_support_unittest.cc b/src/common/linux/linux_libc_support_unittest.cc index 6f4b2da6..7f974af8 100644 --- a/src/common/linux/linux_libc_support_unittest.cc +++ b/src/common/linux/linux_libc_support_unittest.cc @@ -129,6 +129,23 @@ TEST(LinuxLibcSupportTest, strchr) { ASSERT_TRUE(my_strchr("abc", 'a')); ASSERT_TRUE(my_strchr("bcda", 'a')); ASSERT_TRUE(my_strchr("sdfasdf", 'a')); + + static const char abc3[] = "abcabcabc"; + ASSERT_EQ(abc3, my_strchr(abc3, 'a')); +} + +TEST(LinuxLibcSupportTest, strrchr) { + ASSERT_EQ(NULL, my_strrchr("abc", 'd')); + ASSERT_EQ(NULL, my_strrchr("", 'd')); + ASSERT_EQ(NULL, my_strrchr("efghi", 'd')); + + ASSERT_TRUE(my_strrchr("a", 'a')); + ASSERT_TRUE(my_strrchr("abc", 'a')); + ASSERT_TRUE(my_strrchr("bcda", 'a')); + ASSERT_TRUE(my_strrchr("sdfasdf", 'a')); + + static const char abc3[] = "abcabcabc"; + ASSERT_EQ(abc3 + 6, my_strrchr(abc3, 'a')); } TEST(LinuxLibcSupportTest, read_hex_ptr) { @@ -155,3 +172,24 @@ TEST(LinuxLibcSupportTest, read_hex_ptr) { ASSERT_EQ(result, 0x123a); ASSERT_EQ(*last, '-'); } + +TEST(LinuxLibcSupportTest, read_decimal_ptr) { + uintptr_t result; + const char* last; + + last = my_read_decimal_ptr(&result, "0"); + ASSERT_EQ(result, 0); + ASSERT_EQ(*last, 0); + + last = my_read_decimal_ptr(&result, "0123"); + ASSERT_EQ(result, 123); + ASSERT_EQ(*last, 0); + + last = my_read_decimal_ptr(&result, "1234"); + ASSERT_EQ(result, 1234); + ASSERT_EQ(*last, 0); + + last = my_read_decimal_ptr(&result, "01234-"); + ASSERT_EQ(result, 1234); + ASSERT_EQ(*last, '-'); +} -- cgit v1.2.1