Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

sys/string_utils: add memchk() #19686

Merged
merged 4 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions sys/include/string_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
*/

/**
* @defgroup sys_string_utils Utility functions that are missing in
* `string.h`
* @defgroup sys_string_utils Utility functions that are missing in `string.h`
* @ingroup sys
*
* This header provides utility functions that the standard C libs `string.h`
Expand Down Expand Up @@ -92,6 +91,18 @@ static inline void explicit_bzero(void *dest, size_t n_bytes)
*/
ssize_t strscpy(char *dest, const char *src, size_t count);

/**
* @brief Check if the entire buffer is filled with the same byte.
*
* @param[in] data The buffer to probe
* @param[in] c The byte to check of
* @param[in] len Size of the buffer
*
* @return NULL if the entire buffer is filled with @p c
* @return pointer to the first non-matching byte
*/
const void *memchk(const void *data, uint8_t c, size_t len);

#ifdef __cplusplus
}
#endif
Expand Down
13 changes: 13 additions & 0 deletions sys/libc/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,17 @@ ssize_t strscpy(char *dest, const char *src, size_t count)
return -E2BIG;
}
}

const void *memchk(const void *data, uint8_t c, size_t len)
{
const uint8_t *end = (uint8_t *)data + len;
for (const uint8_t *d = data; d != end; ++d) {
if (c != *d) {
return d;
}
}

return NULL;
}

/** @} */
15 changes: 2 additions & 13 deletions sys/net/gnrc/pktbuf_static/gnrc_pktbuf_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "net/gnrc/pktbuf.h"
#include "net/gnrc/nettype.h"
#include "net/gnrc/pkt.h"
#include "string_utils.h"

#include "pktbuf_internal.h"
#include "pktbuf_static.h"
Expand Down Expand Up @@ -61,18 +62,6 @@ static gnrc_pktsnip_t *_create_snip(gnrc_pktsnip_t *next, const void *data, size
gnrc_nettype_t type);
static void *_pktbuf_alloc(size_t size);

static const void *mem_is_set(const void *data, uint8_t c, size_t len)
{
const uint8_t *end = (uint8_t *)data + len;
for (const uint8_t *d = data; d != end; ++d) {
if (c != *d) {
return d;
}
}

return NULL;
}

static inline void _set_pktsnip(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *next,
void *data, size_t size, gnrc_nettype_t type)
{
Expand Down Expand Up @@ -440,7 +429,7 @@ static void *_pktbuf_alloc(size_t size)

const void *mismatch;
if (CONFIG_GNRC_PKTBUF_CHECK_USE_AFTER_FREE &&
(mismatch = mem_is_set(ptr + 1, CANARY, size - sizeof(_unused_t)))) {
(mismatch = memchk(ptr + 1, CANARY, size - sizeof(_unused_t)))) {
printf("[%p] mismatch at offset %"PRIuPTR"/%u (ignoring %u initial bytes that were repurposed)\n",
(void *)ptr, (uintptr_t)mismatch - (uintptr_t)ptr, (unsigned)size, (unsigned)sizeof(_unused_t));
#ifdef MODULE_OD
Expand Down
11 changes: 11 additions & 0 deletions tests/unittests/tests-libc/tests-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,21 @@ static void test_libc_strscpy(void)
TEST_ASSERT_EQUAL_INT(strscpy(buffer, "empty", 0), -E2BIG);
}

static void test_libc_memchk(void)
{
char buffer[32];
memset(buffer, 0xff, sizeof(buffer));
TEST_ASSERT_NULL(memchk(buffer, 0xff, sizeof(buffer)));

buffer[5] = 5;
TEST_ASSERT(memchk(buffer, 0xff, sizeof(buffer)) == &buffer[5]);
}

Test *tests_libc_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_libc_strscpy),
new_TestFixture(test_libc_memchk),
};

EMB_UNIT_TESTCALLER(libc_tests, NULL, NULL, fixtures);
Expand Down