feat: check for oom in string

This commit is contained in:
2025-11-19 20:38:43 +01:00
parent ecf4113621
commit 114dbc2494
4 changed files with 28 additions and 1 deletions

View File

@@ -0,0 +1 @@
# This file is generated by cmake for dependency checking of the CMakeCache.txt file

View File

@@ -7,7 +7,8 @@ add_library(tstd STATIC library.c
include/tstd/string.h include/tstd/string.h
string.c string.c
list.c list.c
include/tstd/list.h) include/tstd/list.h
helper.h)
add_executable(test_tstd_string test/test_string.c) add_executable(test_tstd_string test/test_string.c)

16
helper.h Normal file
View File

@@ -0,0 +1,16 @@
//
// Created by tilok on 19.11.2025.
//
#ifndef TSTD_HELPER_H
#define TSTD_HELPER_H
#include <stdio.h>
#define OOM(ptr) { \
if(ptr == NULL) { \
puts("Out of memory"); \
exit(137); \
} \
}
#endif //TSTD_HELPER_H

View File

@@ -1,4 +1,5 @@
#include "tstd/string.h" #include "tstd/string.h"
#include "helper.h"
size_t str_count_occurrences(const char* str, const char c) { size_t str_count_occurrences(const char* str, const char c) {
size_t i = 0; size_t i = 0;
@@ -28,6 +29,8 @@ size_t str_count_occurrences_of_substring(const char* str, const char* substr) {
size_t str_split_by_char(const char* str, const char delimiter, char*** result) { size_t str_split_by_char(const char* str, const char delimiter, char*** result) {
const size_t size = str_count_occurrences(str, delimiter); const size_t size = str_count_occurrences(str, delimiter);
char** parts = malloc((size+1) * sizeof(char*)); char** parts = malloc((size+1) * sizeof(char*));
OOM(parts);
int curr_part_idx = 0; int curr_part_idx = 0;
int last_part_end = 0; int last_part_end = 0;
int idx = 0; int idx = 0;
@@ -35,6 +38,7 @@ size_t str_split_by_char(const char* str, const char delimiter, char*** result)
if (str[idx] == delimiter) { if (str[idx] == delimiter) {
const int len = idx - last_part_end; const int len = idx - last_part_end;
char* part = malloc(sizeof(char) * (len+1)); char* part = malloc(sizeof(char) * (len+1));
OOM(part);
memcpy(part, &str[last_part_end], len); memcpy(part, &str[last_part_end], len);
part[idx-last_part_end] = '\0'; part[idx-last_part_end] = '\0';
@@ -45,6 +49,7 @@ size_t str_split_by_char(const char* str, const char delimiter, char*** result)
idx++; idx++;
} }
char* part = malloc(sizeof(char) * (idx-last_part_end+1)); char* part = malloc(sizeof(char) * (idx-last_part_end+1));
OOM(part);
memcpy(part, &str[last_part_end], idx-last_part_end); memcpy(part, &str[last_part_end], idx-last_part_end);
part[idx-last_part_end] = '\0'; part[idx-last_part_end] = '\0';
parts[curr_part_idx++] = part; parts[curr_part_idx++] = part;
@@ -64,6 +69,7 @@ size_t str_split_by_substring(const char* str, const char* substr, char*** resul
if (strstr(&str[idx], substr) == &str[idx]) { if (strstr(&str[idx], substr) == &str[idx]) {
const size_t len = idx - last_part_end; const size_t len = idx - last_part_end;
char* part = malloc(sizeof(char) * (len+1)); char* part = malloc(sizeof(char) * (len+1));
OOM(part);
memcpy(part, &str[last_part_end], len); memcpy(part, &str[last_part_end], len);
part[len] = '\0'; part[len] = '\0';
@@ -75,6 +81,7 @@ size_t str_split_by_substring(const char* str, const char* substr, char*** resul
} }
const size_t len = idx - last_part_end; const size_t len = idx - last_part_end;
char* part = malloc(sizeof(char) * (len+1)); char* part = malloc(sizeof(char) * (len+1));
OOM(part);
memcpy(part, &str[last_part_end], len); memcpy(part, &str[last_part_end], len);
part[len] = '\0'; part[len] = '\0';
@@ -105,6 +112,7 @@ char* str_concat(const char* str1, const char* str2) {
const size_t len2 = strlen(str2); const size_t len2 = strlen(str2);
char* result = malloc(sizeof(char) * (len1 + len2 + 1)); char* result = malloc(sizeof(char) * (len1 + len2 + 1));
OOM(result);
memcpy(result, str1, len1); memcpy(result, str1, len1);
memcpy(&result[len1], str2, len2); memcpy(&result[len1], str2, len2);
result[len1 + len2] = '\0'; result[len1 + len2] = '\0';
@@ -126,6 +134,7 @@ char* str_trim(const char* str) {
} }
char* new_str = malloc(sizeof(char) * (end - start + 2)); char* new_str = malloc(sizeof(char) * (end - start + 2));
OOM(new_str);
memcpy(new_str, &str[start], end - start + 1); memcpy(new_str, &str[start], end - start + 1);
new_str[end-start+1] = '\0'; new_str[end-start+1] = '\0';