diff options
author | Marco Paland <marco@paland.com> | 2018-12-05 17:48:36 +0100 |
---|---|---|
committer | Marco Paland <marco@paland.com> | 2018-12-05 17:48:36 +0100 |
commit | d46b3d2b253776a332f0f0a115a5a71a2f34e923 (patch) | |
tree | 30b025ae38b00b24d371060578d37a3f11fb29ef /printf.c | |
parent | 4b60eb6973b384bf12223a923d24865ddbe97778 (diff) | |
download | printf-d46b3d2b253776a332f0f0a115a5a71a2f34e923.tar.gz printf-d46b3d2b253776a332f0f0a115a5a71a2f34e923.tar.bz2 printf-d46b3d2b253776a332f0f0a115a5a71a2f34e923.zip |
chore(printf): cleanup secure strlen() function, added test cases
Diffstat (limited to 'printf.c')
-rw-r--r-- | printf.c | 9 |
1 files changed, 4 insertions, 5 deletions
@@ -139,14 +139,13 @@ static inline void _out_fct(char character, void* buffer, size_t idx, size_t max }
-// internal strlen
+// internal secure strlen
// \return The length of the string (excluding the terminating 0)
// limited by 'max' size if non-zero
-static inline unsigned int _strlen(const char* str, size_t max)
+static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
{
const char* s;
- size_t n = max;
- for (s = str; *s && (max?n--:1); ++s);
+ for (s = str; *s && maxsize--; ++s);
return (unsigned int)(s - str);
}
@@ -649,7 +648,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const case 's' : {
char* p = va_arg(va, char*);
- unsigned int l = _strlen(p, precision);
+ unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);
// pre padding
if (flags & FLAGS_PRECISION) {
l = (l < precision ? l : precision);
|