We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4b60eb6 commit d46b3d2Copy full SHA for d46b3d2
README.md
@@ -28,7 +28,7 @@ Therefore I decided to write an own, final implementation which meets the follow
28
- Support of decimal/floating number representation (with an own fast itoa/ftoa)
29
- Reentrant and thread-safe, malloc free, no static vars/buffers
30
- LINT and compiler L4 warning free, mature, coverity clean, automotive ready
31
- - Extensive test suite (> 340 test cases) passing
+ - Extensive test suite (> 350 test cases) passing
32
- Simply the best *printf* around the net
33
- MIT license
34
printf.c
@@ -139,14 +139,13 @@ static inline void _out_fct(char character, void* buffer, size_t idx, size_t max
139
}
140
141
142
-// internal strlen
+// internal secure strlen
143
// \return The length of the string (excluding the terminating 0)
144
// limited by 'max' size if non-zero
145
-static inline unsigned int _strlen(const char* str, size_t max)
+static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
146
{
147
const char* s;
148
- size_t n = max;
149
- for (s = str; *s && (max?n--:1); ++s);
+ for (s = str; *s && maxsize--; ++s);
150
return (unsigned int)(s - str);
151
152
@@ -649,7 +648,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
649
648
650
case 's' : {
651
char* p = va_arg(va, char*);
652
- unsigned int l = _strlen(p, precision);
+ unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);
653
// pre padding
654
if (flags & FLAGS_PRECISION) {
655
l = (l < precision ? l : precision);
test/test_suite.cpp
@@ -1187,6 +1187,32 @@ TEST_CASE("unknown flag", "[]" ) {
1187
1188
1189
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
1213
+}
1214
1215
1216
TEST_CASE("buffer length", "[]" ) {
1217
char buffer[100];
1218
int ret;
0 commit comments