Skip to content

Commit

Permalink
added length limit for _strlen
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cz7asm committed Dec 5, 2018
1 parent 0d641bc commit b04d559
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit b04d559

Please sign in to comment.