diff options
author | Paul LeoNerd Evans <leonerd@leonerd.org.uk> | 2013-08-26 18:46:01 +0100 |
---|---|---|
committer | Paul LeoNerd Evans <leonerd@leonerd.org.uk> | 2013-08-26 18:46:01 +0100 |
commit | e3d0baa84699dc0fe589fc36e53d09132a0b5925 (patch) | |
tree | d5792da5b4859fdd0a20a2e133c2b10258c87859 /termkey.c | |
parent | e9a1302e4cc82cdb82c46df66bbcbb707f3faa48 (diff) | |
download | termo-e3d0baa84699dc0fe589fc36e53d09132a0b5925.tar.gz termo-e3d0baa84699dc0fe589fc36e53d09132a0b5925.tar.xz termo-e3d0baa84699dc0fe589fc36e53d09132a0b5925.zip |
Bugfix to snprint_cameltospaces() when given a short buffer [thanks Romain Chossart]
Diffstat (limited to 'termkey.c')
-rw-r--r-- | termkey.c | 17 |
1 files changed, 12 insertions, 5 deletions
@@ -198,19 +198,26 @@ static int snprint_cameltospaces(char *str, size_t size, const char *src) { int prev_lower = 0; size_t l = 0; - while(*src && l < size) { + while(*src && l < size - 1) { if(isupper(*src) && prev_lower) { if(str) str[l++] = ' '; - if(l >= size) - return -1; + if(l >= size - 1) + break; } prev_lower = islower(*src); str[l++] = tolower(*src++); } - if(l >= size) - return -1; str[l] = 0; + /* For consistency with snprintf, return the number of bytes that would have + * been written, excluding '\0' */ + while(*src) { + if(isupper(*src) && prev_lower) { + l++; + } + prev_lower = islower(*src); + src++; l++; + } return l; } |