From ec99e9033a55b0806138756fb42dcadec474884a Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sun, 10 Mar 2013 00:29:01 +0100 Subject: [PATCH] pn532_spi: use new connstring_decode() Fix cppcheck warning "Non reentrant function 'strtok' called" --- libnfc/drivers/pn532_spi.c | 81 +++++++++++--------------------------- 1 file changed, 22 insertions(+), 59 deletions(-) diff --git a/libnfc/drivers/pn532_spi.c b/libnfc/drivers/pn532_spi.c index 5824443..a1b9f23 100644 --- a/libnfc/drivers/pn532_spi.c +++ b/libnfc/drivers/pn532_spi.c @@ -54,11 +54,11 @@ # include # include # define msleep(x) do { \ - struct timespec xsleep; \ - xsleep.tv_sec = x / 1000; \ - xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ - nanosleep(&xsleep, NULL); \ - } while (0) + struct timespec xsleep; \ + xsleep.tv_sec = x / 1000; \ + xsleep.tv_nsec = (x - xsleep.tv_sec * 1000) * 1000 * 1000; \ + nanosleep(&xsleep, NULL); \ + } while (0) #else // Needed by Sleep() under Windows # include @@ -144,59 +144,10 @@ pn532_spi_scan(const nfc_context *context, nfc_connstring connstrings[], const s } struct pn532_spi_descriptor { - char port[128]; + char *port; uint32_t speed; }; -static int -pn532_connstring_decode(const nfc_connstring connstring, struct pn532_spi_descriptor *desc) -{ - char *cs = malloc(strlen(connstring) + 1); - if (!cs) { - perror("malloc"); - return -1; - } - strcpy(cs, connstring); - const char *driver_name = strtok(cs, ":"); - if (!driver_name) { - // Parse error - free(cs); - return -1; - } - - if (0 != strcmp(driver_name, PN532_SPI_DRIVER_NAME)) { - // Driver name does not match. - free(cs); - return 0; - } - - const char *port = strtok(NULL, ":"); - if (!port) { - // Only driver name was specified (or parsing error) - free(cs); - return 1; - } - strncpy(desc->port, port, sizeof(desc->port) - 1); - desc->port[sizeof(desc->port) - 1] = '\0'; - - const char *speed_s = strtok(NULL, ":"); - if (!speed_s) { - // speed not specified (or parsing error) - free(cs); - return 2; - } - unsigned long speed; - if (sscanf(speed_s, "%10lu", &speed) != 1) { - // speed_s is not a number - free(cs); - return 2; - } - desc->speed = speed; - - free(cs); - return 3; -} - static void pn532_spi_close(nfc_device *pnd) { @@ -205,6 +156,7 @@ pn532_spi_close(nfc_device *pnd) // Release SPI port spi_close(DRIVER_DATA(pnd)->port); + free(DRIVER_DATA(pnd)->port); pn53x_data_free(pnd); nfc_device_free(pnd); } @@ -213,8 +165,18 @@ static nfc_device * pn532_spi_open(const nfc_context *context, const nfc_connstring connstring) { struct pn532_spi_descriptor ndd; - int connstring_decode_level = pn532_connstring_decode(connstring, &ndd); - + char *speed_s; + int connstring_decode_level = connstring_decode(connstring, PN532_SPI_DRIVER_NAME, NULL, &ndd.port, &speed_s); + if (connstring_decode_level == 3) { + ndd.speed = 0; + if (sscanf(speed_s, "%10"PRIu32, &ndd.speed) != 1) { + // speed_s is not a number + free(ndd.port); + free(speed_s); + return NULL; + } + free(speed_s); + } if (connstring_decode_level < 2) { return NULL; } @@ -231,9 +193,10 @@ pn532_spi_open(const nfc_context *context, const nfc_connstring connstring) log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Invalid SPI port: %s", ndd.port); if (sp == CLAIMED_SPI_PORT) log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "SPI port already claimed: %s", ndd.port); - if ((sp == CLAIMED_SPI_PORT) || (sp == INVALID_SPI_PORT)) + if ((sp == CLAIMED_SPI_PORT) || (sp == INVALID_SPI_PORT)) { + free(ndd.port); return NULL; - + } spi_set_speed(sp, ndd.speed); spi_set_mode(sp, PN532_SPI_MODE);