Compare commits

...

3 Commits

Author SHA1 Message Date
francesco 9b10360acf feat(meson): add library to pkgconf 2026-03-28 18:09:20 +01:00
francesco 449fcb4cba refactor(string): improve string security 2026-03-16 23:32:03 +01:00
francesco 47fc8b50a6 refactor(test): use builtin meson test 2026-03-16 23:22:37 +01:00
11 changed files with 194 additions and 171 deletions
+63 -50
View File
@@ -1,75 +1,88 @@
project(
'testlib',
'c',
version: '0.2',
default_options: ['c_std=c18', 'warning_level=3'],
'testlib',
'c',
version: '0.3',
default_options: ['c_std=c18', 'warning_level=3'],
)
cc = meson.get_compiler('c')
# Add global posix feature macro
add_global_arguments('-D_POSIX_C_SOURCE=200112L', language: 'c')
# Sources without test files
lib_src = files(
'hashmap/myhashmap.c',
'queue/myqueue.c',
'set/myset.c',
'stack/mystack.c',
'string/mystring.c',
'vector/myvector.c',
)
# Test files
test_src = files(
'test/hashmap/hm1.c',
'test/queue/queue1.c',
'test/set/set1.c',
'test/stack/stack1.c',
'test/string/str1.c',
'test/string/str2.c',
'test/string/str3.c',
'test/test.c',
'test/vector/vec1.c',
lib_src = files(
'hashmap/myhashmap.c',
'queue/myqueue.c',
'set/myset.c',
'stack/mystack.c',
'string/mystring.c',
'vector/myvector.c',
)
# Include directories
inc_dir = include_directories('string', 'queue', 'hashmap', 'vector', 'stack', 'set')
if host_machine.system() == 'windows'
win_inc_dir = include_directories('c:/include/')
win_inc_dir = include_directories('c:/include/')
else
win_inc_dir = []
win_inc_dir = []
endif
# Static library
pkg = import('pkgconfig')
myclib_lib = static_library(
'myclib',
lib_src,
include_directories: inc_dir,
dependencies: winsock_dep,
install: true,
'myclib',
lib_src,
include_directories: inc_dir,
install: true,
)
pkg.generate(
myclib_lib,
name: 'myclib',
description: 'My personal C std library.',
)
# Install headers
install_headers(
[
'hashmap/myhashmap.h',
'queue/myqueue.h',
'string/mystring.h',
'vector/myvector.h',
'set/myset.h',
'stack/mystack.h',
],
subdir: 'myclib',
[
'hashmap/myhashmap.h',
'queue/myqueue.h',
'string/mystring.h',
'vector/myvector.h',
'set/myset.h',
'stack/mystack.h',
],
subdir: 'myclib',
)
# Test executable
testlib_exe = executable(
'testlib',
test_src,
include_directories: [inc_dir, win_inc_dir],
dependencies: winsock_dep,
link_with: myclib_lib,
)
# Per-file test registrations
test('testlib', testlib_exe)
test_cases = [
['hashmap_hm1', 'test/hashmap/hm1.c'],
['queue_queue1', 'test/queue/queue1.c'],
['set_set1', 'test/set/set1.c'],
['stack_stack1', 'test/stack/stack1.c'],
['string_str1', 'test/string/str1.c'],
['string_str2', 'test/string/str2.c'],
['string_str3', 'test/string/str3.c'],
['vector_vec1', 'test/vector/vec1.c'],
]
foreach tc : test_cases
test_name = tc[0]
test_source = tc[1]
test_exe = executable(
'test_' + test_name,
test_source,
include_directories: [inc_dir, win_inc_dir],
link_with: myclib_lib,
)
test(test_name, test_exe)
endforeach
+94 -57
View File
@@ -1,5 +1,4 @@
#include "mystring.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdint.h>
@@ -10,8 +9,9 @@
/* Returns the next power of two of a number */
static size_t next_power_two(size_t len) {
if (len == 0)
if (len == 0) {
return 1;
}
size_t p = 1;
while (p < len) {
@@ -36,11 +36,9 @@ string_s *string_new(const char *text, size_t initial_capacity) {
}
str->size = strlen(text);
if (initial_capacity != 0 && initial_capacity < (str->size + 1)) {
/* Can't allocate with this capacity */
free(str);
return NULL;
}
@@ -49,14 +47,12 @@ string_s *string_new(const char *text, size_t initial_capacity) {
/* Calculate the needed capacity */
capacity = next_power_two(str->size + 1);
}
str->capacity = capacity;
/* Allocate data (text) buffer */
str->data = malloc(str->capacity);
if (str->data == NULL) {
free(str);
return NULL;
}
@@ -68,7 +64,6 @@ string_s *string_new(const char *text, size_t initial_capacity) {
if (mtx_init(&str->lock, mtx_recursive) != thrd_success) {
free(str->data);
free(str);
return NULL;
}
@@ -88,12 +83,10 @@ int string_append(string_s *string, const char *text) {
size_t text_len = strlen(text);
if (text_len == 0) {
mtx_unlock(&string->lock);
return 0;
}
size_t new_size = string->size + text_len;
/* Check if we need to resize */
if (new_size + 1 > string->capacity) {
size_t new_capacity = next_power_two(new_size + 1);
@@ -101,10 +94,8 @@ int string_append(string_s *string, const char *text) {
void *new_data = realloc(string->data, new_capacity);
if (!new_data) {
mtx_unlock(&string->lock);
return -1;
}
string->data = new_data;
string->capacity = new_capacity;
}
@@ -113,7 +104,6 @@ int string_append(string_s *string, const char *text) {
memcpy(string->data + string->size, text, text_len);
string->size = new_size;
string->data[string->size] = '\0';
mtx_unlock(&string->lock);
return 0;
@@ -124,37 +114,59 @@ int string_extend(string_s *destination, string_s *source) {
return -1;
}
/* Lock both strings. The recursive mutex handles the self-extend case
* (destination == source) without deadlock. */
if (mtx_lock(&destination->lock) != thrd_success) {
return -1;
}
if (mtx_lock(&source->lock) != thrd_success) {
if (destination != source && mtx_lock(&source->lock) != thrd_success) {
mtx_unlock(&destination->lock);
return -1;
}
size_t need = destination->size + source->size;
if (need > destination->capacity) {
if (source->size > SIZE_MAX - destination->size - 1) {
mtx_unlock(&destination->lock);
if (destination != source) {
mtx_unlock(&source->lock);
}
return -1;
}
size_t destination_size = destination->size;
size_t source_size = source->size;
size_t need = destination_size + source_size;
size_t needed_capacity = need + 1;
if (needed_capacity > destination->capacity) {
/* Reallocate destination data buffer */
destination->capacity = next_power_two(need);
char *tmp = realloc(destination->data, destination->capacity);
size_t new_capacity = next_power_two(needed_capacity);
char *tmp = realloc(destination->data, new_capacity);
if (tmp == NULL) {
mtx_unlock(&destination->lock);
mtx_unlock(&source->lock);
if (destination != source) {
mtx_unlock(&source->lock);
}
return -1;
}
destination->data = tmp;
destination->capacity = new_capacity;
}
/* self-extend requires memmove due to overlapping source/destination ranges */
if (destination == source) {
memmove(destination->data + destination_size, destination->data, source_size);
} else {
memcpy(destination->data + destination_size, source->data, source_size);
}
/* Copy memory from source data buffer */
memcpy(destination->data + destination->size, source->data, source->size);
destination->size = need;
destination->data[destination->size] = '\0';
mtx_unlock(&destination->lock);
mtx_unlock(&source->lock);
if (destination != source) {
mtx_unlock(&source->lock);
}
return 0;
}
@@ -168,7 +180,6 @@ void string_free(string_s *string) {
}
mtx_destroy(&string->lock);
free(string);
}
@@ -182,7 +193,6 @@ size_t string_len(string_s *string) {
}
size_t len = string->size;
mtx_unlock(&string->lock);
return len;
@@ -198,7 +208,6 @@ size_t string_cap(string_s *string) {
}
size_t cap = string->capacity;
mtx_unlock(&string->lock);
return cap;
@@ -253,7 +262,6 @@ char *string_copy(string_s *string) {
memcpy(cpy, string->data, string->size);
cpy[string->size] = '\0';
mtx_unlock(&string->lock);
return cpy;
@@ -264,20 +272,23 @@ int string_compare(string_s *s1, string_s *s2) {
return -123;
}
/* Lock both strings. The recursive mutex handles the s1 == s2 case
* without deadlock. */
if (mtx_lock(&s1->lock) != thrd_success) {
return -123;
}
if (mtx_lock(&s2->lock) != thrd_success) {
if (s1 != s2 && mtx_lock(&s2->lock) != thrd_success) {
mtx_unlock(&s1->lock);
return -123;
}
int ret = strcmp(s1->data, s2->data);
mtx_unlock(&s1->lock);
mtx_unlock(&s2->lock);
if (s1 != s2) {
mtx_unlock(&s2->lock);
}
return ret;
}
@@ -293,7 +304,6 @@ int string_clear(string_s *string) {
memset(string->data, 0, string->size);
string->size = 0;
mtx_unlock(&string->lock);
return 0;
@@ -339,7 +349,6 @@ int string_tolower(string_s *string) {
static void build_lps(int *lps, const char *substring, size_t sub_len) {
int len = 0;
size_t i = 1;
while (i < sub_len) {
if (substring[i] == substring[len]) {
lps[i++] = ++len;
@@ -362,19 +371,26 @@ int string_find(string_s *string, const char *substring) {
return -1;
}
/* Handle empty case */
/* Handle empty string case */
if (strcmp(string->data, "") == 0) {
mtx_unlock(&string->lock);
return -1;
}
size_t sub_len = strlen(substring);
if (sub_len == 0) {
mtx_unlock(&string->lock);
return 0;
}
int *lps = (int *)malloc(sub_len * sizeof(int));
if (lps == NULL) {
mtx_unlock(&string->lock);
return -1;
}
memset(lps, 0, sub_len * sizeof(int));
build_lps(lps, substring, sub_len);
size_t i = 0; /* string iterator */
size_t j = 0; /* substring iterator */
while (i < string->size) {
@@ -384,7 +400,6 @@ int string_find(string_s *string, const char *substring) {
if (j == sub_len) {
free(lps);
mtx_unlock(&string->lock);
return (int)(i - j);
}
} else {
@@ -397,7 +412,6 @@ int string_find(string_s *string, const char *substring) {
}
free(lps);
mtx_unlock(&string->lock);
return -1;
@@ -410,14 +424,22 @@ string_s *string_format(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
size_t len = vsnprintf(NULL, 0, fmt, args) + 1;
int formatted_len = vsnprintf(NULL, 0, fmt, args);
va_end(args);
if (formatted_len < 0) {
return NULL;
}
size_t len = (size_t)formatted_len + 1;
string_s *str = string_new("", len);
if (str == NULL) {
return NULL;
}
va_start(args, fmt);
vsnprintf(str->data, str->capacity, fmt, args);
va_end(args);
str->size = (size_t)formatted_len;
return str;
}
@@ -427,38 +449,44 @@ int string_insert(string_s *string, size_t index, const char *text) {
return -1;
}
if (index > string->size) {
return -1;
}
if (mtx_lock(&string->lock) != thrd_success) {
return -1;
}
size_t text_len = strlen(text);
size_t new_size = string->size + text_len;
if (index > string->size) {
mtx_unlock(&string->lock);
return -1;
}
size_t text_len = strlen(text);
if (text_len > SIZE_MAX - string->size - 1) {
mtx_unlock(&string->lock);
return -1;
}
size_t new_size = string->size + text_len;
if (new_size + 1 > string->capacity) {
/* Reallocate buffer */
size_t new_cap = next_power_two(new_size + 1);
void *tmp = realloc(string->data, new_cap);
if (tmp == NULL) {
mtx_unlock(&string->lock);
return -1;
}
string->data = tmp;
string->capacity = new_cap;
}
/* Shift bytes */
memmove((char *)string->data + ((index + text_len) * sizeof(char)),
(char *)string->data + (index * sizeof(char)), string->size - index + 1);
/* Insert new text */
memcpy((char *)string->data + (index * sizeof(char)), text, text_len);
string->size = new_size;
/* Ensure null termination */
string->data[string->size] = '\0';
mtx_unlock(&string->lock);
return 0;
@@ -469,19 +497,21 @@ int string_remove(string_s *string, size_t index, size_t length) {
return -1;
}
if (index + length > string->size) {
return -1;
}
if (mtx_lock(&string->lock) != thrd_success) {
return -1;
}
if (index > string->size || length > string->size - index) {
mtx_unlock(&string->lock);
return -1;
}
size_t tail_size = string->size - (index + length);
memmove((char *)string->data + (index * sizeof(char)),
(char *)string->data + ((index + length) * sizeof(char)), string->size - length);
(char *)string->data + ((index + length) * sizeof(char)), tail_size + 1);
string->size -= length;
string->data[string->size] = '\0';
mtx_unlock(&string->lock);
return 0;
@@ -492,23 +522,30 @@ int string_replace(string_s *string, const char *old_text, const char *new_text)
return -1;
}
size_t old_len = strlen(old_text);
if (old_len == 0) {
return -1;
}
if (mtx_lock(&string->lock) != thrd_success) {
return -1;
}
int pos;
size_t old_len = strlen(old_text);
int ret = 0;
while (1) {
pos = string_find(string, old_text);
if (pos == -1) {
break;
}
string_remove(string, pos, old_len);
string_insert(string, pos, new_text);
if (string_remove(string, (size_t)pos, old_len) != 0 ||
string_insert(string, (size_t)pos, new_text) != 0) {
ret = -1;
break;
}
}
mtx_unlock(&string->lock);
return 0;
return ret;
}
+1 -1
View File
@@ -48,7 +48,7 @@ void my_free_value(void *value) {
free(mct);
}
void test_hm1(void) {
int main(void) {
/* Allocate a new hashmap */
/* Pass your custom hash, equal and free functions */
/* This hashmap will contain names as keys and a custom type as value */
+1 -1
View File
@@ -1,7 +1,7 @@
#include "../queue/myqueue.h"
#include <assert.h>
void test_queue1(void) {
int main(void) {
/* Allocate a new queue */
queue_s *queue = queue_new(3, sizeof(int));
assert(queue != NULL);
+1 -1
View File
@@ -2,7 +2,7 @@
#include <assert.h>
void test_set1(void) {
int main(void) {
set_s *set = set_new(sizeof(int));
assert(set != NULL);
+1 -1
View File
@@ -2,7 +2,7 @@
#include <assert.h>
#include <stdlib.h>
void test_stack1(void) {
int main(void) {
stack_s *stack = stack_new(32, sizeof(int));
int num = 10;
stack_push(stack, &num);
+1 -1
View File
@@ -4,7 +4,7 @@
#include <stdlib.h>
#include <string.h>
void test_str1(void) {
int main(void) {
size_t length;
size_t capacity;
char *c_str;
+19 -1
View File
@@ -2,7 +2,7 @@
#include <assert.h>
#include <string.h>
void test_str2(void) {
int main(void) {
string_s *s1 = string_new("Hello, world!", 0);
assert(s1 != NULL);
string_s *s2 = string_new("Hello, world!", 0);
@@ -27,6 +27,21 @@ void test_str2(void) {
assert(string_len(s1) == 50);
assert(string_cap(s1) == 64);
/* Regression: when need == capacity, extend still needs room for '\0'. */
string_s *tight_dst = string_new("abc", 4);
string_s *tight_src = string_new("d", 0);
assert(tight_dst != NULL);
assert(tight_src != NULL);
assert(string_extend(tight_dst, tight_src) == 0);
assert(strcmp(string_cstr(tight_dst), "abcd") == 0);
assert(string_cap(tight_dst) >= 5);
/* Self-extend should be safe and deterministic. */
string_s *self = string_new("xy", 0);
assert(self != NULL);
assert(string_extend(self, self) == 0);
assert(strcmp(string_cstr(self), "xyxy") == 0);
/* Find substring in string */
int pos = string_find(s1, " is ");
assert(pos == 11);
@@ -34,4 +49,7 @@ void test_str2(void) {
string_free(s1);
string_free(s2);
string_free(extend_me);
string_free(tight_dst);
string_free(tight_src);
string_free(self);
}
+12 -1
View File
@@ -2,7 +2,7 @@
#include <assert.h>
#include <string.h>
void test_str3(void) {
int main(void) {
/* Make a new string from format */
string_s *s = string_format("My name is %s (%d)", "John", 21);
assert(strcmp(s->data, "My name is John (21)") == 0);
@@ -16,6 +16,16 @@ void test_str3(void) {
/* Test delete */
string_remove(ms, 5, 5);
assert(strcmp(ms->data, "Hello!") == 0);
assert(string_remove(ms, string_len(ms), 1) == -1);
string_s *remove_mid = string_new("abcdef", 0);
assert(remove_mid != NULL);
assert(string_remove(remove_mid, 2, 2) == 0);
assert(strcmp(remove_mid->data, "abef") == 0);
assert(string_remove(remove_mid, 2, 2) == 0);
assert(strcmp(remove_mid->data, "ab") == 0);
assert(string_remove(remove_mid, 0, 2) == 0);
assert(strcmp(remove_mid->data, "") == 0);
/* Test replace */
string_s *replace_me = string_new("My car doesn't bark!", 0);
@@ -25,5 +35,6 @@ void test_str3(void) {
string_free(ms);
string_free(s);
string_free(remove_mid);
string_free(replace_me);
}
-56
View File
@@ -1,56 +0,0 @@
/**
* Ignore this file
*/
#include <assert.h>
#include <stdio.h>
void test_hm1(void);
void test_queue1(void);
void test_str1(void);
void test_str2(void);
void test_str3(void);
void test_vec1(void);
void test_stack1(void);
void test_set1(void);
int main(void) {
puts("==== [Running Hashmap tests] ====");
test_hm1();
puts("OK!");
puts("");
puts("==== [Running Queue tests] ====");
test_queue1();
puts("OK!");
puts("");
puts("==== [Running String tests] ====");
test_str1();
test_str2();
test_str3();
puts("OK!");
puts("");
puts("==== [Running Vector tests] ====");
test_vec1();
puts("OK!");
puts("");
puts("==== [Running Stack tests] ====");
test_stack1();
puts("OK!");
puts("");
puts("==== [Running Set tests] ====");
test_set1();
puts("OK!");
puts("");
return 0;
}
+1 -1
View File
@@ -30,7 +30,7 @@ int my_cmp(const void *a, const void *b) {
return ma->age - mb->age;
}
void test_vec1(void) {
int main(void) {
/* Allocate a new vector */
size_t elem_size = sizeof(my_elem_s);
vec_s *v = vec_new(10, elem_size);