Skip to content

Commit d46b3d2

Browse files
committed
chore(printf): cleanup secure strlen() function, added test cases
1 parent 4b60eb6 commit d46b3d2

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Therefore I decided to write an own, final implementation which meets the follow
2828
- Support of decimal/floating number representation (with an own fast itoa/ftoa)
2929
- Reentrant and thread-safe, malloc free, no static vars/buffers
3030
- LINT and compiler L4 warning free, mature, coverity clean, automotive ready
31-
- Extensive test suite (> 340 test cases) passing
31+
- Extensive test suite (> 350 test cases) passing
3232
- Simply the best *printf* around the net
3333
- MIT license
3434

printf.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,13 @@ static inline void _out_fct(char character, void* buffer, size_t idx, size_t max
139139
}
140140

141141

142-
// internal strlen
142+
// internal secure strlen
143143
// \return The length of the string (excluding the terminating 0)
144144
// limited by 'max' size if non-zero
145-
static inline unsigned int _strlen(const char* str, size_t max)
145+
static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
146146
{
147147
const char* s;
148-
size_t n = max;
149-
for (s = str; *s && (max?n--:1); ++s);
148+
for (s = str; *s && maxsize--; ++s);
150149
return (unsigned int)(s - str);
151150
}
152151

@@ -649,7 +648,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
649648

650649
case 's' : {
651650
char* p = va_arg(va, char*);
652-
unsigned int l = _strlen(p, precision);
651+
unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);
653652
// pre padding
654653
if (flags & FLAGS_PRECISION) {
655654
l = (l < precision ? l : precision);

test/test_suite.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,32 @@ TEST_CASE("unknown flag", "[]" ) {
11871187
}
11881188

11891189

1190+
TEST_CASE("string length", "[]" ) {
1191+
char buffer[100];
1192+
1193+
test::sprintf(buffer, "%.4s", "This is a test");
1194+
REQUIRE(!strcmp(buffer, "This"));
1195+
1196+
test::sprintf(buffer, "%.4s", "test");
1197+
REQUIRE(!strcmp(buffer, "test"));
1198+
1199+
test::sprintf(buffer, "%.7s", "123");
1200+
REQUIRE(!strcmp(buffer, "123"));
1201+
1202+
test::sprintf(buffer, "%.7s", "");
1203+
REQUIRE(!strcmp(buffer, ""));
1204+
1205+
test::sprintf(buffer, "%.4s%.2s", "123456", "abcdef");
1206+
REQUIRE(!strcmp(buffer, "1234ab"));
1207+
1208+
test::sprintf(buffer, "%.4.2s", "123456");
1209+
REQUIRE(!strcmp(buffer, ".2s"));
1210+
1211+
test::sprintf(buffer, "%.*s", 3, "123456");
1212+
REQUIRE(!strcmp(buffer, "123"));
1213+
}
1214+
1215+
11901216
TEST_CASE("buffer length", "[]" ) {
11911217
char buffer[100];
11921218
int ret;

0 commit comments

Comments
 (0)