1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-12-04 14:46:45 -05:00

Correct a subtle off-by-one error in copying in l_strfmon()

The bug in l_strfmon() is that bufsize is NOT the length of the string,
hence do not include the usual "+ 1" for the NUL byte.  Also correct some
comparisons between signed and unsigned integers.
This commit is contained in:
John Zaitseff 2011-07-20 21:47:20 +10:00
parent b3f1820418
commit 6a6c31035a

View File

@ -359,6 +359,8 @@ ssize_t l_strfmon (char *restrict s, size_t maxsize,
char *p;
int spc;
assert(maxsize > (unsigned int) symlen);
// Count number of leading spaces
for (p = s, spc = 0; *p == ' '; p++, spc++)
;
@ -372,7 +374,7 @@ ssize_t l_strfmon (char *restrict s, size_t maxsize,
} else {
// Make space for currency symbol, then copy it
memmove(s + symlen - spc, s, maxsize - (symlen - spc) + 1);
memmove(s + symlen - spc, s, maxsize - (symlen - spc));
s[maxsize - 1] = '\0';
for ( ; *sym != '\0'; sym++, s++) {
@ -380,7 +382,7 @@ ssize_t l_strfmon (char *restrict s, size_t maxsize,
*s = *sym;
}
ret = MIN(ret + symlen - spc, maxsize - 1);
ret = MIN((unsigned int) ret + symlen - spc, maxsize - 1);
}
}
}