mirror of
https://git.zap.org.au/git/trader.git
synced 2025-02-02 15:08:13 -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:
parent
b3f1820418
commit
6a6c31035a
@ -359,6 +359,8 @@ ssize_t l_strfmon (char *restrict s, size_t maxsize,
|
|||||||
char *p;
|
char *p;
|
||||||
int spc;
|
int spc;
|
||||||
|
|
||||||
|
assert(maxsize > (unsigned int) symlen);
|
||||||
|
|
||||||
// Count number of leading spaces
|
// Count number of leading spaces
|
||||||
for (p = s, spc = 0; *p == ' '; p++, spc++)
|
for (p = s, spc = 0; *p == ' '; p++, spc++)
|
||||||
;
|
;
|
||||||
@ -372,7 +374,7 @@ ssize_t l_strfmon (char *restrict s, size_t maxsize,
|
|||||||
} else {
|
} else {
|
||||||
// Make space for currency symbol, then copy it
|
// 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';
|
s[maxsize - 1] = '\0';
|
||||||
|
|
||||||
for ( ; *sym != '\0'; sym++, s++) {
|
for ( ; *sym != '\0'; sym++, s++) {
|
||||||
@ -380,7 +382,7 @@ ssize_t l_strfmon (char *restrict s, size_t maxsize,
|
|||||||
*s = *sym;
|
*s = *sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = MIN(ret + symlen - spc, maxsize - 1);
|
ret = MIN((unsigned int) ret + symlen - spc, maxsize - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user