BugID:23251508: core: fix lwip_itoa()
commit
Author: David Girault
Date:   Mon Jan 8 21:37:31 2018 +0100
See discussion in bug
@@ -199,10 +199,8 @@ void
lwip_itoa(char* result, size_t bufsize, int number)
{
char *res = result;
- char *tmp = result;
- size_t res_left = bufsize;
- size_t result_len;
- int n = (number > 0) ? number : -number;
+ char *tmp = result + bufsize - 1;
+ int n = (number >= 0) ? number : -number;
/* handle invalid bufsize */
if (bufsize < 2) {
@@ -212,34 +210,31 @@ lwip_itoa(char* result, size_t bufsize, int number)
return;
}
- /* ensure output string is zero terminated */
- result[bufsize-1] = 0;
- result_len = 1;
- /* create the string in a temporary buffer since we don't know how long
- it will get */
- tmp = &result[bufsize-2];
- while ((n != 0) && (result_len < (result_len - 1))) {
+ /* First, add sign */
+ if (number < 0) {
+ *res++ = '-';
+ }
+ /* Then create the string from the end and stop if buffer full,
+ and ensure output string is zero terminated */
+ *tmp = 0;
+ while ((n != 0) && (tmp > res)) {
char val = (char)('0' + (n % 10));
- *tmp = val;
tmp--;
+ *tmp = val;
n = n / 10;
- result_len++;
}
-
- /* output sign first */
- if (number < 0) {
- *res = '-';
- res++;
- res_left--;
- }
- if (result_len > res_left) {
+ if (n) {
/* buffer is too small */
- result[0] = '.';
- result[1] = 0;
+ *result = 0;
return;
}
- /* copy from temporary buffer to output buffer */
- memmove(res, tmp+1, result_len);
-
+ if (*tmp == 0) {
+ /* Nothing added? */
+ *res++ = '0';
+ *res++ = 0;
+ return;
+ }
+ /* move from temporary buffer to output buffer (sign is not moved) */
+ memmove(res, tmp, (result + bufsize) - tmp);
}
#endif
