aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcz7asm <cz7asm@gmail.com>2018-12-05 12:32:46 +0100
committerGitHub <noreply@github.com>2018-12-05 12:32:46 +0100
commitb04d55907f040e376f96aad950d1e8d3723bbb07 (patch)
tree4de5c1b2d81d041a1f780acf8bfccd949125602b
parent0d641bcd9c84627b31662050ddcc0b79ccd1a09b (diff)
downloadprintf-b04d55907f040e376f96aad950d1e8d3723bbb07.tar.gz
printf-b04d55907f040e376f96aad950d1e8d3723bbb07.tar.bz2
printf-b04d55907f040e376f96aad950d1e8d3723bbb07.zip
added length limit for _strlen
I wanted the limit specifier for strings (e.g. "%16.s") to be usable in situations when zero termination isn't guaranteed. As a simple fix I added lenght limitation to _strlen.
-rw-r--r--printf.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/printf.c b/printf.c
index a04b42b..93a2f0f 100644
--- a/printf.c
+++ b/printf.c
@@ -141,10 +141,12 @@ static inline void _out_fct(char character, void* buffer, size_t idx, size_t max
// internal strlen
// \return The length of the string (excluding the terminating 0)
-static inline unsigned int _strlen(const char* str)
+// limited by 'max' size if non-zero
+static inline unsigned int _strlen(const char* str, size_t max)
{
const char* s;
- for (s = str; *s; ++s);
+ size_t n = max;
+ for (s = str; *s && (max?n--:1); ++s);
return (unsigned int)(s - str);
}