aboutsummaryrefslogtreecommitdiff
path: root/src/common/linux
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/linux')
-rw-r--r--src/common/linux/linux_libc_support.cc10
-rw-r--r--src/common/linux/linux_libc_support.h2
-rw-r--r--src/common/linux/linux_libc_support_unittest.cc12
3 files changed, 24 insertions, 0 deletions
diff --git a/src/common/linux/linux_libc_support.cc b/src/common/linux/linux_libc_support.cc
index 845729c9..08b0325e 100644
--- a/src/common/linux/linux_libc_support.cc
+++ b/src/common/linux/linux_libc_support.cc
@@ -138,6 +138,16 @@ const char* my_strrchr(const char* haystack, char needle) {
return ret;
}
+void* my_memchr(const void* src, int needle, size_t src_len) {
+ const unsigned char* p = (const unsigned char*)src;
+ const unsigned char* p_end = p + src_len;
+ for (; p < p_end; ++p) {
+ if (*p == needle)
+ return (void*)p;
+ }
+ return NULL;
+}
+
// Read a hex value
// result: (output) the resulting value
// s: a string
diff --git a/src/common/linux/linux_libc_support.h b/src/common/linux/linux_libc_support.h
index 3429600b..ec5a8d6b 100644
--- a/src/common/linux/linux_libc_support.h
+++ b/src/common/linux/linux_libc_support.h
@@ -77,6 +77,8 @@ extern const char* my_read_decimal_ptr(uintptr_t* result, const char* s);
extern void my_memset(void* ip, char c, size_t len);
+extern void* my_memchr(const void* src, int 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
diff --git a/src/common/linux/linux_libc_support_unittest.cc b/src/common/linux/linux_libc_support_unittest.cc
index 5d06d17e..adadfed4 100644
--- a/src/common/linux/linux_libc_support_unittest.cc
+++ b/src/common/linux/linux_libc_support_unittest.cc
@@ -154,6 +154,18 @@ TEST(LinuxLibcSupportTest, strrchr) {
ASSERT_EQ(abc3 + 6, my_strrchr(abc3, 'a'));
}
+TEST(LinuxLibcSupportTest, memchr) {
+ ASSERT_EQ(NULL, my_memchr("abc", 'd', 3));
+ ASSERT_EQ(NULL, my_memchr("abcd", 'd', 3));
+ ASSERT_EQ(NULL, my_memchr("a", 'a', 0));
+
+ static const char abc3[] = "abcabcabc";
+ ASSERT_EQ(abc3, my_memchr(abc3, 'a', 3));
+ ASSERT_EQ(abc3, my_memchr(abc3, 'a', 9));
+ ASSERT_EQ(abc3+1, my_memchr(abc3, 'b', 9));
+ ASSERT_EQ(abc3+2, my_memchr(abc3, 'c', 9));
+}
+
TEST(LinuxLibcSupportTest, read_hex_ptr) {
uintptr_t result;
const char* last;