Commits

Dean Hall  committed 8e58f9f

Fixes issue 220
Replaced simple uses of snprintf with new sli_ltoa10.

  • Participants
  • Parent commits 62f72bb
  • Branches v10

Comments (0)

Files changed (5)

File src/vm/heap.c

     static int n = 0;
     uint16_t s;
     uint32_t i;
-    char filename[32];
+    char filename = "pmheapdump0N.bin";
     FILE *fp;
 
-    snprintf(filename, 32, "pmheapdump%02d.bin", n++);
+    filename[11] = '0' + n++;
     fp = fopen(filename, "wb");
 
     /* magic : PMDUMP for little endian or PMUDMP for big endian */

File src/vm/int.c

 PmReturn_t
 int_print(pPmObj_t pint)
 {
-    /* 2^31-1 has 10 decimal digits, plus sign and zero byte */
-    uint8_t tBuffer[10 + 1 + 1];
-    uint8_t bytesWritten;
-    uint8_t k;
     PmReturn_t retval = PM_RET_OK;
+    uint8_t buf[12];
 
     C_ASSERT(pint != C_NULL);
 
         return retval;
     }
 
-    /* #196: Changed to use snprintf */
-    bytesWritten =
-        snprintf((char *)&tBuffer, 12, "%li", (long int)((pPmInt_t)pint)->val);
+    sli_ltoa10(((pPmInt_t)pint)->val, buf);
+    sli_puts(buf);
 
-
-    /* Sanity check */
-    C_ASSERT(bytesWritten != 0);
-    C_ASSERT(bytesWritten < sizeof(tBuffer));
-
-    for (k = (uint8_t)0; k < bytesWritten; k++)
-    {
-        retval = plat_putByte(tBuffer[k]);
-        PM_RETURN_IF_ERROR(retval);
-    }
     return PM_RET_OK;
 }
 

File src/vm/obj.c

     switch (OBJ_GET_TYPE(pobj))
     {
         case OBJ_TYPE_INT:
-            bytesWritten = snprintf((char *)&tBuffer, sizeof(tBuffer), "%li",
-                                    (long)((pPmInt_t)pobj)->val);
+            sli_ltoa10(((pPmInt_t)pobj)->val, tBuffer);
             retval = string_new(&pcstr, r_pstr);
             break;
 

File src/vm/sli.c

         dest++;
     }
 }
+
+
+void sli_puts(uint8_t * s)
+{
+    uint8_t *ps = s;
+    uint8_t c;
+
+    c = *ps;
+    ps++;
+    while (c != '\0')
+    {
+        plat_putByte(c);
+        c = *ps;
+        ps++;
+    }
+}
+
+
+uint8_t * sli_ltoa10(int32_t value, uint8_t *buf)
+{
+    int32_t const decimal_places[] = { 1000000000, 100000000, 10000000, 1000000,
+                                       100000, 10000, 1000, 100, 10, 1 };
+    int32_t decimal_place;
+    int32_t number;
+    uint8_t c;
+    uint8_t printed_one = 0;
+    uint8_t i;
+    uint8_t j;
+
+    number = value;
+    if (number == 0)
+    {
+        buf[0] = '0';
+        buf[1] = '\0';
+        return buf;
+    }
+
+    j = 0;
+    if (number < 0)
+    {
+        buf[0] = '-';
+        j++;
+        number = -number;
+    }
+
+    for (i = 0; i < 10; i++)
+    {
+        decimal_place = decimal_places[i];
+        c = '0';
+        while (number >= decimal_place)
+        {
+            number -= decimal_place;
+            c++;
+        }
+        if ((c != '0') || printed_one)
+        {
+            buf[j++] = c;
+            printed_one = 1;
+        }
+    }
+    buf[j] = '\0';
+
+    return buf;
+}

File src/vm/sli.h

  */
 void sli_memset(unsigned char *dest, const char val, unsigned int n);
 
+/**
+ * Prints a string to stdout (using plat_putByte)
+ *
+ * @param s Pointer to the C string to print
+ */
+void sli_puts(uint8_t * s);
+
+/**
+ * Formats a 32-bit signed int as a decimal value.
+ *
+ * @param value the 32-bit signed value
+ * @param buf a pointer to where the formted string goes
+ * @return a pointer to the string. 
+ */
+uint8_t * sli_ltoa10(int32_t value, uint8_t *buf);
+
 #endif /* __SLI_H__ */