eina: Fix bug in eina_convert_itoa

The current implementation of eina_convert_itoa cannot convert INT_MIN.

When the input number is negative, the function negates it and this is
an undefined behavior for INT_MIN since -INT_MIN cannot be represented
in a signed int.

@fix T1062

Signed-off-by: Cedric BAIL <cedric.bail@samsung.com>
This commit is contained in:
Sylvain Laperche 2014-03-07 10:58:36 +08:00 committed by Cedric BAIL
parent e6faf98868
commit 53e0a2d6bd
2 changed files with 8 additions and 3 deletions

View File

@ -157,14 +157,13 @@ eina_convert_itoa(int n, char *s)
if (n < 0)
{
n = -n;
*s++ = '-';
r = 1;
}
do {
s[i++] = n % 10 + '0';
} while ((n /= 10) > 0);
s[i++] = abs(n % 10) + '0';
} while (n /= 10);
s[i] = '\0';

View File

@ -24,6 +24,7 @@
#include <string.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include "eina_suite.h"
#include "Eina.h"
@ -31,6 +32,7 @@
START_TEST(eina_convert_simple)
{
char tmp[128];
char ref[128];
fail_if(eina_convert_itoa(0, tmp) != 1);
fail_if(strcmp(tmp, "0") != 0);
@ -47,6 +49,10 @@ START_TEST(eina_convert_simple)
fail_if(eina_convert_itoa(10000000, tmp) != 8);
fail_if(strcmp(tmp, "10000000") != 0);
snprintf(ref, sizeof (ref), "%d", INT_MIN);
fail_if(eina_convert_itoa(INT_MIN, tmp) != (int) strlen(ref));
fail_if(strcmp(tmp, ref) != 0);
fail_if(eina_convert_xtoa(0, tmp) != 1);
fail_if(strcmp(tmp, "0") != 0);