0.3.0: converge relibc to upstream 0.6.0 + Red Bear patches

This commit is contained in:
2026-07-06 19:13:08 +03:00
parent 1a0edd8eeb
commit 4ef7e57571
1466 changed files with 75236 additions and 13644 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ int main(void) {
puts("Correct memrchr");
char arr2[51];
memset(arr2, 0, 51); // Compiler builtin, should work
memccpy((void *)arr2, (void *)arr, 1, 100);
memccpy((void *)arr2, (void *)arr, 1, 51);
if (arr[50] != 1) {
puts("Incorrect memccpy");
exit(EXIT_FAILURE);
+53
View File
@@ -0,0 +1,53 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test_helpers.h"
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
int main(void) {
const uint8_t UNTOUCHED_BYTE = 0x00;
const uint8_t TOUCHED_BYTE = 0xff;
// In order to fully exercise the implementation, this should be at least 3 times the largest possible chunk size
const size_t BUFFER_LEN = 64;
uint8_t *s1_buffer = malloc(BUFFER_LEN);
uint8_t *s2_buffer = malloc(BUFFER_LEN);
// Loop through all possible combinations of s1 and s2 alignments and slice length within the buffers allocated
for (size_t s1_offset = 0; s1_offset < BUFFER_LEN; s1_offset++) {
for (size_t s2_offset = 0; s2_offset < BUFFER_LEN; s2_offset++) {
size_t n_max = BUFFER_LEN - MAX(s1_offset, s2_offset);
for (size_t n = 1; n <= n_max; n++) {
// Clear buffers
memset(s1_buffer, UNTOUCHED_BYTE, BUFFER_LEN);
memset(s2_buffer, UNTOUCHED_BYTE, BUFFER_LEN);
// Fill s2 subslice
memset(s2_buffer + s2_offset, TOUCHED_BYTE, n);
// Do the actual memcpy of the slice of interest
memcpy(s1_buffer + s1_offset, s2_buffer + s2_offset, n);
// Check that area below the slice of interest is untouched
for (size_t i = 0; i < s1_offset; i++) {
assert(s1_buffer[i] == UNTOUCHED_BYTE);
}
// Check that the slice of interest was copied
assert(memcmp(s1_buffer + s1_offset, s2_buffer + s2_offset, n) == 0);
// Check that area above the slice of interest is untouched
for (size_t i = s1_offset + n; i < BUFFER_LEN; i++) {
assert(s1_buffer[i] == UNTOUCHED_BYTE);
}
}
}
}
free(s1_buffer);
free(s2_buffer);
}
+31
View File
@@ -0,0 +1,31 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "test_helpers.h"
int main(void) {
uint8_t haystack[] = {1, 1, 2, 1, 2, 3, 1, 2, 3, 4};
uint8_t present_needle[] = {1, 2, 3};
uint8_t absent_needle[] = {1, 2, 3, 4, 5};
uint8_t long_needle[] = {1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1};
size_t haystacklen = sizeof(haystack);
size_t present_needlelen = sizeof(present_needle);
size_t absent_needlelen = sizeof(absent_needle);
size_t long_needlelen = sizeof(long_needle);
uint8_t *present_needle_match_ptr = memmem(haystack, haystacklen, present_needle, present_needlelen);
assert(present_needle_match_ptr == haystack + 3);
uint8_t *absent_needle_match_ptr = memmem(haystack, haystacklen, absent_needle, absent_needlelen);
assert(absent_needle_match_ptr == NULL);
// Explicitly specified to return haystack for needlelen == 0.
uint8_t *zero_needle_match_ptr = memmem(haystack, haystacklen, present_needle, 0);
assert(zero_needle_match_ptr == haystack);
uint8_t *long_needle_match_ptr = memmem(haystack, haystacklen, long_needle, long_needlelen);
assert(long_needle_match_ptr == NULL);
}
+26
View File
@@ -0,0 +1,26 @@
#include <string.h>
#include <assert.h>
void test_stpcpy(const char *src, char *dest) {
char *end = stpcpy(dest, src);
assert(strcmp(dest, src) == 0);
assert(*end == '\0');
assert(end == dest + strlen(dest));
}
int main() {
char dest[20];
test_stpcpy("Hello, World!", dest);
// Test case 2: Empty string
test_stpcpy("", dest);
// Test case 3: String with special characters
test_stpcpy("Special chars: !@#$%^&*()", dest);
// Test case 4: String with spaces
test_stpcpy("A string with spaces", dest);
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
#include <string.h>
#include <assert.h>
int main() {
// Define some test strings
const char *src = "Hello, World!";
char dest[50];
// Test 1: Copy exactly 5 characters from src to dest
char *result = stpncpy(dest, src, 5);
dest[5] = '\0'; // Ensure the destination string is null-terminated
assert(strcmp(dest, "Hello") == 0); // Verify the string in dest
assert(result == &dest[5]); // Ensure the return pointer points to the null terminator
// Test 2: Copy 15 characters from src to dest (more than the length of src)
result = stpncpy(dest, src, 15);
dest[13] = '\0'; // Ensure the destination string is null-terminated
assert(strcmp(dest, "Hello, World!") == 0); // Verify the string in dest
assert(result == &dest[13]); // Ensure the return pointer points to the null terminator
// Test 3: Copy 3 characters from src to dest
result = stpncpy(dest, src, 3);
dest[3] = '\0'; // Ensure the destination string is null-terminated
assert(strcmp(dest, "Hel") == 0); // Verify the string in dest
assert(result == &dest[3]); // Ensure the return pointer points to the null terminator
// Test 4: Copy 0 characters from src to dest
result = stpncpy(dest, src, 0);
dest[0] = '\0'; // Ensure the destination is explicitly null-terminated
assert(dest[0] == '\0'); // Ensure the destination is an empty string
assert(result == dest); // Ensure the return pointer points to the start of dest
// Test 5: Copy exactly the length of the source string
result = stpncpy(dest, src, strlen(src));
dest[strlen(src)] = '\0'; // Ensure the destination string is null-terminated
assert(strcmp(dest, "Hello, World!") == 0); // Verify the string in dest
assert(result == &dest[strlen(src)]); // Ensure the return pointer points to the null terminator
return 0;
}
+2 -1
View File
@@ -6,5 +6,6 @@
int main(void) {
printf("%s\n", strchr("hello", 'e')); // should be ello
printf("%s\n", strchr("world", 'l')); // should be ld
printf("%i\n", strchr("world", 0) == NULL); // should be 1
printf("%s\n", strchr("world", '\0')); // should be an empty, nul-terminated string
printf("%p\n", strchr("world", 'x')); // should be a null pointer
}
+31
View File
@@ -0,0 +1,31 @@
#include <string.h>
#include <assert.h>
int main() {
// Test 1: Character is present in the string
const char *str1 = "Hello, World!";
const char *result1 = strchrnul(str1, 'o');
assert(result1 == &str1[4]); // 'o' is at position 4 in "Hello, World!"
// Test 2: Character is not in the string (should return the null terminator)
const char *str2 = "Hello, World!";
const char *result2 = strchrnul(str2, 'z');
assert(result2 == &str2[13]); // 'z' is not present, so it returns the null terminator
// Test 3: Character is the first character in the string
const char *str3 = "abcdef";
const char *result3 = strchrnul(str3, 'a');
assert(result3 == &str3[0]); // 'a' is at position 0
// Test 4: Character is the last character in the string
const char *str4 = "abcdef";
const char *result4 = strchrnul(str4, 'f');
assert(result4 == &str4[5]); // 'f' is at position 5, the last character
// Test 5: Searching for the null terminator itself
const char *str5 = "abcdef";
const char *result5 = strchrnul(str5, '\0');
assert(result5 == &str5[6]); // The null terminator is at position 6 (end of the string)
return 0;
}
+10
View File
@@ -16,4 +16,14 @@ int main(void) {
dst[19] = 0;
strncpy(dst, "strncpy should work here too", 10);
puts(dst);
// The string should be properly terminated regardless
char ndst[28];
size_t r = strlcpy(ndst, "strlcpy works!", 28);
puts(ndst);
printf("copied %lu\n", r);
r = strlcat(ndst, " and strlcat!", 28);
puts(ndst);
printf("copied %lu\n", r);
}
+3
View File
@@ -48,6 +48,8 @@ int main(void) {
exit(EXIT_FAILURE);
}
// no strnlen_s on glibc
#ifndef __GLIBC__
dest1_len = strnlen_s(dest1, 6);
printf("%d\n", dest1_len);
if(dest1_len != 6) {
@@ -68,6 +70,7 @@ int main(void) {
puts("strnlen_s(NULL, 100) failed");
exit(EXIT_FAILURE);
}
#endif
return 0;
}
+68
View File
@@ -0,0 +1,68 @@
#include <string.h>
#include <assert.h>
int main() {
// Test case 1: Basic case with multiple tokens
char str1[] = "apple,orange,banana";
char *delim = ",";
char *token = str1;
char *result = NULL;
// First token should be "apple"
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
// Second token should be "orange"
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "orange") == 0);
// Third token should be "banana"
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "banana") == 0);
// No more tokens
result = strsep(&token, delim);
assert(result == NULL);
// Test case 2: Empty string
char str2[] = "";
token = str2;
result = strsep(&token, delim);
// Test case 3: String with no delimiter
char str3[] = "apple";
token = str3;
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
assert(token == NULL); // No more tokens
// Test case 4: String starts with delimiter
char str4[] = ",apple,orange";
token = str4;
result = strsep(&token, delim);
assert(result != NULL && strlen(result) == 0); // First token should be an empty string ("")
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
// Test case 5: Multiple delimiters in a row
char str5[] = "apple,,orange";
token = str5;
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
result = strsep(&token, delim);
assert(result != NULL && strlen(result) == 0); // Empty token due to consecutive delimiters
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "orange") == 0);
// Test case 6: Delimiters at the end of the string
char str6[] = "apple,orange,";
token = str6;
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "apple") == 0);
result = strsep(&token, delim);
assert(result != NULL && strcmp(result, "orange") == 0);
result = strsep(&token, delim);
return 0;
}