Skip to content

Commit c93d0d0

Browse files
committed
Use a machine-verified implementation of x_strlcpy()
1 parent 8075a4b commit c93d0d0

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ x64
1919
/config.status
2020
Makefile
2121
/stamp-h1
22+
!/tests/string_utils_test/Makefile

tests/string_utils_test/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
string_utils_test: string_utils_test.o x_strlcpy.o
2+
$(CXX) -o $@ string_utils_test.o x_strlcpy.o -lboost_unit_test_framework-mt -lstdc++
3+
4+
string_utils_test.o: ../../string_utils.h ../../x_strlcpy.c
5+
$(CXX) -c -o $@ -I ../.. string_utils_test.cpp
6+
7+
x_strlcpy.o: ../../string_utils.h ../../x_strlcpy.c
8+
$(CXX) -c -o $@ -I ../.. ../../x_strlcpy.c

x_strlcpy.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
/* -*- coding: utf-8; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: t -*- */
22

3-
// This code and all comments, written by Daniel Trebbien, are hereby entered into the Public Domain by their author.
3+
/* This code and all comments, written by Daniel Trebbien, are hereby entered into the Public Domain by their author. */
44

5-
#include <assert.h>
65
#include <string.h>
76

87
#include "string_utils.h"
98

9+
/* This implementation is machine-verified:
10+
https://github.com/dtrebbien/verlibc/blob/daea83474b36dc251751d100d2ba347d1aa906d6/src/string/strlcpy.c */
1011
size_t x_strlcpy(char *__restrict dest, const char *__restrict src, size_t dest_len)
1112
{
1213
if (dest_len == 0) {
1314
return strlen(src);
1415
} else {
1516
char *const dest_str_end = dest + dest_len - 1;
1617
char *__restrict d = dest;
18+
const char *__restrict s = src;
1719

18-
while (d != dest_str_end)
19-
{
20-
if ((*d++ = *src++) == '\0') {
21-
assert(*(src - 1) == '\0');
22-
assert(*(d - 1) == '\0');
20+
while (d != dest_str_end) {
21+
if ((*d++ = *s++) == '\0') {
2322
return (d - 1 - dest);
2423
}
2524
}
2625

2726
*d = '\0';
28-
return (dest_len - 1) + strlen(src);
27+
return (dest_len - 1) + strlen(s);
2928
}
3029
}

0 commit comments

Comments
 (0)