Fix off-by-one buffer size error (detected by PC-lint)
[libftdi] / src / ftdi.c
index 1616fdb..dca79e1 100644 (file)
@@ -1091,6 +1091,16 @@ static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
 }
 
 /**
+ * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test
+ * Do not use, it's only for the unit test framework
+ **/
+int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
+                                 unsigned short *value, unsigned short *index)
+{
+    return ftdi_convert_baudrate(baudrate, ftdi, value, index);
+}
+
+/**
     Sets the chip baud rate
 
     \param ftdi pointer to ftdi_context
@@ -2208,7 +2218,7 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
         ftdi_error_return(-3, "No connected device or device not yet opened");
 
     eeprom->vendor_id = 0x0403;
-    eeprom->use_serial = USE_SERIAL_NUM;
+    eeprom->use_serial = 1;
     if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
             (ftdi->type == TYPE_R))
         eeprom->product_id = 0x6001;
@@ -2243,6 +2253,25 @@ int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
         if (eeprom->product)
             strcpy(eeprom->product, product);
     }
+    else
+    {
+        const char* default_product;
+        switch(ftdi->type)
+        {
+        case TYPE_AM:    default_product = "AM"; break;
+        case TYPE_BM:    default_product = "BM"; break;
+        case TYPE_2232C: default_product = "Dual RS232"; break;
+        case TYPE_R:     default_product = "FT232R USB UART"; break;
+        case TYPE_2232H: default_product = "Dual RS232-HS"; break;
+        case TYPE_4232H: default_product = "FT4232H"; break;
+        case TYPE_232H:  default_product = "Single-RS232-HS"; break;
+        default:
+        ftdi_error_return(-3, "Unknown chip type");
+        }
+        eeprom->product = malloc(strlen(default_product) +1);
+        if (eeprom->product)
+            strcpy(eeprom->product, default_product);
+    }
 
     if (eeprom->serial)
         free (eeprom->serial);
@@ -2559,7 +2588,7 @@ int ftdi_eeprom_build(struct ftdi_context *ftdi)
 
     if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
     {
-        if (eeprom->use_serial == USE_SERIAL_NUM )
+        if (eeprom->use_serial)
             output[0x0A] |= USE_SERIAL_NUM;
         else
             output[0x0A] &= ~USE_SERIAL_NUM;
@@ -2874,7 +2903,7 @@ int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
     eeprom->in_is_isochronous  = buf[0x0A]&0x01;
     eeprom->out_is_isochronous = buf[0x0A]&0x02;
     eeprom->suspend_pull_downs = buf[0x0A]&0x04;
-    eeprom->use_serial         = buf[0x0A] & USE_SERIAL_NUM;
+    eeprom->use_serial         = (buf[0x0A] & USE_SERIAL_NUM)?1:0;
     eeprom->use_usb_version    = buf[0x0A] & USE_USB_VERSION_BIT;
 
     // Addr 0C: USB version low byte when 0x0A