Skip to content

Commit

Permalink
Add support for having no malloc function.
Browse files Browse the repository at this point in the history
Fixes #99.
  • Loading branch information
sheredom committed Jul 4, 2022
1 parent f2ee5b6 commit 044e468
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/cmake.yml
Expand Up @@ -64,3 +64,8 @@ jobs:
working-directory: ${{github.workspace}}/build
shell: bash
run: if [ "${{ matrix.os }}" == "windows-latest" ]; then cd ${{ matrix.type }}; fi; ./utf8_test

- name: Test No Malloc
working-directory: ${{github.workspace}}/build
shell: bash
run: if [ "${{ matrix.os }}" == "windows-latest" ]; then cd ${{ matrix.type }}; fi; ./utf8_no_malloc_test
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Expand Up @@ -32,6 +32,10 @@ add_executable(utf8_test
main.c
)

add_executable(utf8_no_malloc_test
no_malloc.c
)

add_executable(utf8_test_c90 test.c)
if("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
target_compile_options(utf8_test_c90 PUBLIC "-std=c90")
Expand Down
14 changes: 7 additions & 7 deletions test/main.c
Expand Up @@ -798,7 +798,7 @@ UTEST(utf8ndup, ascii_larger) {
free(dup);
}

utf8_int8_t *allocate_from_buffer(utf8_int8_t *user_data, size_t n) {
static utf8_int8_t *allocate_from_buffer(utf8_int8_t *user_data, size_t n) {
return user_data;
}

Expand Down Expand Up @@ -1638,18 +1638,18 @@ UTEST(utf8makevalid, invalid_replacement) {
}

UTEST(utf8nvalid, exactly_2_bytes) {
const char terminated[] = "\xc2\xa3";
ASSERT_EQ(utf8nvalid(terminated, 2), NULL);
const char terminated[] = "\xc2\xa3";
ASSERT_EQ(utf8nvalid(terminated, 2), NULL);
}

UTEST(utf8nvalid, exactly_3_bytes) {
const char terminated[] = "\xe1\xbd\xb6";
ASSERT_EQ(utf8nvalid(terminated, 3), NULL);
const char terminated[] = "\xe1\xbd\xb6";
ASSERT_EQ(utf8nvalid(terminated, 3), NULL);
}

UTEST(utf8nvalid, exactly_4_bytes) {
const char terminated[] = "\xf0\x90\x8d\x88";
ASSERT_EQ(utf8nvalid(terminated, 4), NULL);
const char terminated[] = "\xf0\x90\x8d\x88";
ASSERT_EQ(utf8nvalid(terminated, 4), NULL);
}

UTEST_MAIN();
64 changes: 64 additions & 0 deletions test/no_malloc.c
@@ -0,0 +1,64 @@
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>

// include the unit testing framework
#include "utest.h"

// include the header we are testing
#define UTF8_NO_STD_MALLOC
#include "utf8.h"

UTEST(no_malloc_utf8dup, ascii) {
void *const dup = utf8dup("1234567890");
ASSERT_FALSE(dup);
}

UTEST(no_malloc_utf8ndup, ascii) {
void *const dup = utf8ndup("1234567890", 4);
ASSERT_FALSE(dup);
}

static utf8_int8_t *allocate_from_buffer(utf8_int8_t *user_data, size_t n) {
return user_data;
}

UTEST(no_malloc_utf8dup_ex, ascii) {
char user_data[1024];
void *const dup = utf8dup_ex("1234567890", allocate_from_buffer, user_data);
ASSERT_TRUE(dup);
ASSERT_EQ(dup, user_data);
ASSERT_EQ(10, utf8len(dup));
}

UTEST(no_malloc_utf8ndup_ex, ascii) {
char user_data[1024];
void *const dup =
utf8ndup_ex("1234567890", 4, allocate_from_buffer, user_data);
ASSERT_TRUE(dup);
ASSERT_EQ(dup, user_data);
ASSERT_EQ(4, utf8len(dup));
}

UTEST_MAIN();
8 changes: 8 additions & 0 deletions utf8.h
Expand Up @@ -484,7 +484,11 @@ utf8_int8_t *utf8dup_ex(const utf8_int8_t *src,
if (alloc_func_ptr) {
n = alloc_func_ptr(user_data, bytes);
} else {
#if !defined(UTF8_NO_STD_MALLOC)
n = (utf8_int8_t *)malloc(bytes);
#else
return utf8_null;
#endif
}

if (utf8_null == n) {
Expand Down Expand Up @@ -711,7 +715,11 @@ utf8_int8_t *utf8ndup_ex(const utf8_int8_t *src, size_t n,
if (alloc_func_ptr) {
c = alloc_func_ptr(user_data, bytes + 1);
} else {
#if !defined(UTF8_NO_STD_MALLOC)
c = (utf8_int8_t *)malloc(bytes + 1);
#else
c = utf8_null;
#endif
}

if (utf8_null == c) {
Expand Down

0 comments on commit 044e468

Please sign in to comment.