--------------------- PatchSet 10307 Date: 2007/12/23 16:58:56 Author: adri Branch: s27_adri Tag: (none) Log: Reimplement xitoa() to not use sprintf(). This implementation handles negative numbers. Members: lib/util.c:1.23->1.23.12.1 test-suite/check_util.c:1.1->1.1.2.1 Index: squid/lib/util.c =================================================================== RCS file: /cvsroot/squid-sf//squid/lib/util.c,v retrieving revision 1.23 retrieving revision 1.23.12.1 diff -u -r1.23 -r1.23.12.1 --- squid/lib/util.c 1 Apr 2007 15:08:13 -0000 1.23 +++ squid/lib/util.c 23 Dec 2007 16:58:56 -0000 1.23.12.1 @@ -1,6 +1,6 @@ /* - * $Id: util.c,v 1.23 2007/04/01 15:08:13 squidadm Exp $ + * $Id: util.c,v 1.23.12.1 2007/12/23 16:58:56 adri Exp $ * * DEBUG: * AUTHOR: Harvest Derived @@ -727,12 +727,26 @@ } /* integer to string */ +/* equiv to: snprintf(buf, sizeof(buf), "%d", num) */ const char * xitoa(int num) { - static char buf[24]; /* 2^64 = 18446744073709551616 */ - snprintf(buf, sizeof(buf), "%d", num); - return buf; + int j = 30; + int isneg = 0; + static char buf[32]; /* 2^64 = 18446744073709551616 */ + if (num < 0) { + isneg = 1; + num = abs(num); + } + + buf[31] = '\0'; + do { + buf[j--] = '0' + (num % 10); + num /= 10; + } while (num > 0 && j > 0); + if (isneg) + buf[j--] = '-'; + return buf + j + 1; } /* A default failure notifier when the main program hasn't installed any */ --- /dev/null Mon Dec 24 01:21:59 2007 +++ squid/test-suite/check_util.c Mon Dec 24 01:21:59 2007 @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +#include "../include/util.h" + +char const * w_space = " \r\n\t"; +int shutting_down; +time_t squid_curtime; + +void +check_xitoa(int i) +{ + printf("xitoa(%d): %s\n", i, xitoa(i)); +} + +int +main(int argc, const char *argv[]) +{ + check_xitoa(0); + check_xitoa(1); + check_xitoa(130284); + check_xitoa(1108425801); + check_xitoa(-5); + exit(0); +}