* src/pic16/glue.c: fixed bug #983491 - "Merge duplicate strings
authorborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Fri, 19 Oct 2007 19:38:41 +0000 (19:38 +0000)
committerborutr <borutr@4a8a32a2-be11-0410-ad9d-d568d2c75423>
Fri, 19 Oct 2007 19:38:41 +0000 (19:38 +0000)
  function is ineffective" for pic16 tareget
* support/scripts/listerr.c: corrected include path
* device/lib/_itoa.c: fixed bug #1806402 - _itoa and _uitoa leak to
  adjacent memory

git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4938 4a8a32a2-be11-0410-ad9d-d568d2c75423

ChangeLog
device/lib/_itoa.c

index e64f2beeecf1af0dd07df34c7f5c8c4da43e3a0c..0a32df8585cc574161e6d9db535f51fd2f329b4f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,8 @@
        * src/pic16/glue.c: fixed bug #983491 - "Merge duplicate strings
          function is ineffective" for pic16 tareget
        * support/scripts/listerr.c: corrected include path 
+       * device/lib/_itoa.c: fixed bug #1806402 - _itoa and _uitoa leak to
+         adjacent memory
 
 2007-10-18 Maarten Brock <sourceforge.brock AT dse.nl>
 
index f89ba9e497c05a84b3a9ed6fadbd861d57ebcc57..40b28a1a331c4503dce09ddb87ed44e2cfb5f33d 100644 (file)
  radix  ->  Base of value (e.g.: 2 for binary, 10 for decimal, 16 for hex)
 ---------------------------------------------------------------------------*/
 
-#define NUMBER_OF_DIGITS 16
-
 void _uitoa(unsigned int value, char* string, unsigned char radix)
 {
-unsigned char index, i;
-/* char buffer[NUMBER_OF_DIGITS]; */ /* space for NUMBER_OF_DIGITS + '\0' */
-
-  index = NUMBER_OF_DIGITS;
-  i = 0;
+  signed char index = 0, i = 0;
 
+  /* generate the number in reverse order */
   do {
-    string[--index] = '0' + (value % radix);
-    if ( string[index] > '9') string[index] += 'A' - '9' - 1;
+    string[index] = '0' + (value % radix);
+    if (string[index] > '9')
+        string[index] += 'A' - '9' - 1;
     value /= radix;
+    ++index;
   } while (value != 0);
 
-  do {
-    string[i++] = string[index++];
-  } while ( index < NUMBER_OF_DIGITS );
+  /* null terminate the string */
+  string[index--] = '\0';
 
-  string[i] = 0; /* string terminator */
+  /* reverse the order of digits */
+  while (index > i) {
+    char tmp = string[i];
+    string[i] = string[index];
+    string[index] = tmp;
+    ++i;
+    --index;
+  }
 }
 
 void _itoa(int value, char* string, unsigned char radix)