This is an automated email from Gerrit.

Jan Matyas ([email protected]) just uploaded a new patch set to Gerrit, which 
you can find at http://openocd.zylin.com/6182

-- gerrit

commit 3b227d92882a42d0176ab15c027023b40e9e62d1
Author: Jan Matyas <[email protected]>
Date:   Fri Apr 23 10:47:17 2021 +0200

    flash/nor: datatype fix - make buf_size in flash_driver.info unsigned
    
    1) Fix of the buf_size argument in "info" callback of "struct
    flash_driver" - it should be unsigned.
    
    2) Multiple fixes of buffer-overruns-to-be in the struct flash_driver
    info callbacks. These were caused by an improper use of the snprintf's
    return value.
    
    Change-Id: I42ab8a8018d01f9af43c5ba49f650c3cb5d31dcb
    Signed-off-by: Jan Matyas <[email protected]>

diff --git a/src/flash/nor/ambiqmicro.c b/src/flash/nor/ambiqmicro.c
index 1c4dce8..11d07b1 100644
--- a/src/flash/nor/ambiqmicro.c
+++ b/src/flash/nor/ambiqmicro.c
@@ -161,7 +161,7 @@ FLASH_BANK_COMMAND_HANDLER(ambiqmicro_flash_bank_command)
        return ERROR_OK;
 }
 
-static int get_ambiqmicro_info(struct flash_bank *bank, char *buf, int 
buf_size)
+static int get_ambiqmicro_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct ambiqmicro_flash_bank *ambiqmicro_info = bank->driver_priv;
        int printed;
diff --git a/src/flash/nor/at91sam4.c b/src/flash/nor/at91sam4.c
index d4326e4..760b1ee 100644
--- a/src/flash/nor/at91sam4.c
+++ b/src/flash/nor/at91sam4.c
@@ -2656,7 +2656,7 @@ static int sam4_GetDetails(struct sam4_bank_private 
*pPrivate)
        return ERROR_OK;
 }
 
-static int sam4_info(struct flash_bank *bank, char *buf, int buf_size)
+static int sam4_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct sam4_bank_private *pPrivate;
        int k = bank->size / 1024;
diff --git a/src/flash/nor/at91sam7.c b/src/flash/nor/at91sam7.c
index 3d8fee1..22a0042 100644
--- a/src/flash/nor/at91sam7.c
+++ b/src/flash/nor/at91sam7.c
@@ -978,22 +978,22 @@ static int at91sam7_probe(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int get_at91sam7_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_at91sam7_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
-       int printed;
+       int ret;
        struct at91sam7_flash_bank *at91sam7_info = bank->driver_priv;
 
        if (at91sam7_info->cidr == 0)
                return ERROR_FLASH_BANK_NOT_PROBED;
 
-       printed = snprintf(buf, buf_size,
+       ret = snprintf(buf, buf_size,
                        "\n at91sam7 driver information: Chip is %s\n",
                        at91sam7_info->target_name);
 
-       buf += printed;
-       buf_size -= printed;
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
 
-       printed = snprintf(buf,
+       ret = snprintf(buf,
                        buf_size,
                        " Cidr: 0x%8.8" PRIx32 " | Arch: 0x%4.4x | Eproc: %s | 
Version: 0x%3.3x | "
                        "Flashsize: 0x%8.8" PRIx32 "\n",
@@ -1003,28 +1003,28 @@ static int get_at91sam7_info(struct flash_bank *bank, 
char *buf, int buf_size)
                        at91sam7_info->cidr_version,
                        bank->size);
 
-       buf += printed;
-       buf_size -= printed;
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
 
-       printed = snprintf(buf, buf_size,
+       ret = snprintf(buf, buf_size,
                        " Master clock (estimated): %u KHz | External clock: %u 
KHz\n",
                        (unsigned)(at91sam7_info->mck_freq / 1000),
                        (unsigned)(at91sam7_info->ext_freq / 1000));
 
-       buf += printed;
-       buf_size -= printed;
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
 
-       printed = snprintf(buf,
+       ret = snprintf(buf,
                        buf_size,
                        " Pagesize: %i bytes | Lockbits(%u): %i 0x%4.4x | Pages 
in lock region: %i\n",
                        at91sam7_info->pagesize,
                        bank->num_sectors,
                        at91sam7_info->num_lockbits_on,
                        at91sam7_info->lockbits,
-                       
at91sam7_info->pages_per_sector*at91sam7_info->num_lockbits_on);
+                       at91sam7_info->pages_per_sector * 
at91sam7_info->num_lockbits_on);
 
-       buf += printed;
-       buf_size -= printed;
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
 
        snprintf(buf, buf_size,
                " Securitybit: %i | Nvmbits(%i): %i 0x%1.1x\n",
diff --git a/src/flash/nor/ath79.c b/src/flash/nor/ath79.c
index 9a4595f..b1cfa29 100644
--- a/src/flash/nor/ath79.c
+++ b/src/flash/nor/ath79.c
@@ -875,7 +875,7 @@ static int ath79_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int get_ath79_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_ath79_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct ath79_flash_bank *ath79_info = bank->driver_priv;
 
diff --git a/src/flash/nor/atsamv.c b/src/flash/nor/atsamv.c
index 8f1450b..46cfe8d 100644
--- a/src/flash/nor/atsamv.c
+++ b/src/flash/nor/atsamv.c
@@ -607,7 +607,7 @@ static int samv_write(struct flash_bank *bank, const 
uint8_t *buffer,
        return ERROR_OK;
 }
 
-static int samv_get_info(struct flash_bank *bank, char *buf, int buf_size)
+static int samv_get_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct samv_flash_bank *samv_info = bank->driver_priv;
        if (!samv_info->probed) {
diff --git a/src/flash/nor/avrf.c b/src/flash/nor/avrf.c
index dd3d077..99bf1eb 100644
--- a/src/flash/nor/avrf.c
+++ b/src/flash/nor/avrf.c
@@ -367,7 +367,7 @@ static int avrf_auto_probe(struct flash_bank *bank)
        return avrf_probe(bank);
 }
 
-static int avrf_info(struct flash_bank *bank, char *buf, int buf_size)
+static int avrf_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct target *target = bank->target;
        struct avr_common *avr = target->arch_info;
diff --git a/src/flash/nor/bluenrg-x.c b/src/flash/nor/bluenrg-x.c
index 57aebc5..142fd48 100644
--- a/src/flash/nor/bluenrg-x.c
+++ b/src/flash/nor/bluenrg-x.c
@@ -428,7 +428,7 @@ static int bluenrgx_auto_probe(struct flash_bank *bank)
 }
 
 /* This method must return a string displaying information about the bank */
-static int bluenrgx_get_info(struct flash_bank *bank, char *buf, int buf_size)
+static int bluenrgx_get_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct bluenrgx_flash_bank *bluenrgx_info = bank->driver_priv;
        int mask_number, cut_number;
diff --git a/src/flash/nor/cc26xx.c b/src/flash/nor/cc26xx.c
index 5565aeb..8a8d07a 100644
--- a/src/flash/nor/cc26xx.c
+++ b/src/flash/nor/cc26xx.c
@@ -498,7 +498,7 @@ static int cc26xx_auto_probe(struct flash_bank *bank)
        return retval;
 }
 
-static int cc26xx_info(struct flash_bank *bank, char *buf, int buf_size)
+static int cc26xx_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct cc26xx_bank *cc26xx_bank = bank->driver_priv;
        int printed = 0;
@@ -530,7 +530,7 @@ static int cc26xx_info(struct flash_bank *bank, char *buf, 
int buf_size)
                "%s device: ICEPick ID 0x%08" PRIx32 ", USER ID 0x%08" PRIx32 
"\n",
                device, cc26xx_bank->icepick_id, cc26xx_bank->user_id);
 
-       if (printed >= buf_size)
+       if ((unsigned)printed >= buf_size)
                return ERROR_BUF_TOO_SMALL;
 
        return ERROR_OK;
diff --git a/src/flash/nor/cc3220sf.c b/src/flash/nor/cc3220sf.c
index 5427bd3..8e182bc 100644
--- a/src/flash/nor/cc3220sf.c
+++ b/src/flash/nor/cc3220sf.c
@@ -477,13 +477,15 @@ static int cc3220sf_auto_probe(struct flash_bank *bank)
        return retval;
 }
 
-static int cc3220sf_info(struct flash_bank *bank, char *buf, int buf_size)
+static int cc3220sf_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        int printed;
 
        printed = snprintf(buf, buf_size, "CC3220SF with 1MB internal flash\n");
 
-       if (printed >= buf_size)
+       if (printed <= 0)
+               return ERROR_FAIL;
+       if ((unsigned)printed >= buf_size)
                return ERROR_BUF_TOO_SMALL;
 
        return ERROR_OK;
diff --git a/src/flash/nor/cfi.c b/src/flash/nor/cfi.c
index c9eb38b..7ba0d55 100644
--- a/src/flash/nor/cfi.c
+++ b/src/flash/nor/cfi.c
@@ -728,7 +728,7 @@ static int cfi_read_0002_pri_ext(struct flash_bank *bank)
                return cfi_read_spansion_pri_ext(bank);
 }
 
-static int cfi_spansion_info(struct flash_bank *bank, char *buf, int buf_size)
+static int cfi_spansion_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        int printed;
        struct cfi_flash_bank *cfi_info = bank->driver_priv;
@@ -763,7 +763,7 @@ static int cfi_spansion_info(struct flash_bank *bank, char 
*buf, int buf_size)
        return ERROR_OK;
 }
 
-static int cfi_intel_info(struct flash_bank *bank, char *buf, int buf_size)
+static int cfi_intel_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        int printed;
        struct cfi_flash_bank *cfi_info = bank->driver_priv;
@@ -2992,9 +2992,9 @@ int cfi_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-int cfi_get_info(struct flash_bank *bank, char *buf, int buf_size)
+int cfi_get_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
-       int printed;
+       int ret;
        struct cfi_flash_bank *cfi_info = bank->driver_priv;
 
        if (cfi_info->qry[0] == 0xff) {
@@ -3003,34 +3003,38 @@ int cfi_get_info(struct flash_bank *bank, char *buf, 
int buf_size)
        }
 
        if (!cfi_info->not_cfi)
-               printed = snprintf(buf, buf_size, "\nCFI flash: ");
+               ret = snprintf(buf, buf_size, "\nCFI flash: ");
        else
-               printed = snprintf(buf, buf_size, "\nnon-CFI flash: ");
-       buf += printed;
-       buf_size -= printed;
+               ret = snprintf(buf, buf_size, "\nnon-CFI flash: ");
 
-       printed = snprintf(buf, buf_size, "mfr: 0x%4.4x, id:0x%4.4x\n\n",
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
+
+       ret = snprintf(buf, buf_size, "mfr: 0x%4.4x, id:0x%4.4x\n\n",
                        cfi_info->manufacturer, cfi_info->device_id);
-       buf += printed;
-       buf_size -= printed;
 
-       printed = snprintf(buf, buf_size, "qry: '%c%c%c', pri_id: 0x%4.4x, 
pri_addr: "
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
+
+       ret = snprintf(buf, buf_size, "qry: '%c%c%c', pri_id: 0x%4.4x, 
pri_addr: "
                        "0x%4.4x, alt_id: 0x%4.4x, alt_addr: 0x%4.4x\n",
                        cfi_info->qry[0], cfi_info->qry[1], cfi_info->qry[2],
                        cfi_info->pri_id, cfi_info->pri_addr, cfi_info->alt_id, 
cfi_info->alt_addr);
-       buf += printed;
-       buf_size -= printed;
 
-       printed = snprintf(buf, buf_size, "Vcc min: %x.%x, Vcc max: %x.%x, "
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
+
+       ret = snprintf(buf, buf_size, "Vcc min: %x.%x, Vcc max: %x.%x, "
                        "Vpp min: %u.%x, Vpp max: %u.%x\n",
                        (cfi_info->vcc_min & 0xf0) >> 4, cfi_info->vcc_min & 
0x0f,
                        (cfi_info->vcc_max & 0xf0) >> 4, cfi_info->vcc_max & 
0x0f,
                        (cfi_info->vpp_min & 0xf0) >> 4, cfi_info->vpp_min & 
0x0f,
                        (cfi_info->vpp_max & 0xf0) >> 4, cfi_info->vpp_max & 
0x0f);
-       buf += printed;
-       buf_size -= printed;
 
-       printed = snprintf(buf, buf_size, "typ. word write timeout: %u us, "
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
+
+       ret = snprintf(buf, buf_size, "typ. word write timeout: %u us, "
                        "typ. buf write timeout: %u us, "
                        "typ. block erase timeout: %u ms, "
                        "typ. chip erase timeout: %u ms\n",
@@ -3038,10 +3042,11 @@ int cfi_get_info(struct flash_bank *bank, char *buf, 
int buf_size)
                        1 << cfi_info->buf_write_timeout_typ,
                        1 << cfi_info->block_erase_timeout_typ,
                        1 << cfi_info->chip_erase_timeout_typ);
-       buf += printed;
-       buf_size -= printed;
 
-       printed = snprintf(buf,
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
+
+       ret = snprintf(buf,
                        buf_size,
                        "max. word write timeout: %u us, "
                        "max. buf write timeout: %u us, max. "
@@ -3056,16 +3061,18 @@ int cfi_get_info(struct flash_bank *bank, char *buf, 
int buf_size)
                        (1 <<
                         cfi_info->chip_erase_timeout_max) *
                        (1 << cfi_info->chip_erase_timeout_typ));
-       buf += printed;
-       buf_size -= printed;
 
-       printed = snprintf(buf, buf_size, "size: 0x%" PRIx32 ", interface desc: 
%i, "
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
+
+       ret = snprintf(buf, buf_size, "size: 0x%" PRIx32 ", interface desc: %i, 
"
                        "max buffer write size: 0x%x\n",
                        cfi_info->dev_size,
                        cfi_info->interface_desc,
                        1 << cfi_info->max_buf_write_size);
-       buf += printed;
-       buf_size -= printed;
+
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
 
        switch (cfi_info->pri_id) {
            case 1:
diff --git a/src/flash/nor/cfi.h b/src/flash/nor/cfi.h
index eceb9a4..5eb0900 100644
--- a/src/flash/nor/cfi.h
+++ b/src/flash/nor/cfi.h
@@ -160,7 +160,7 @@ int cfi_protect(struct flash_bank *bank, int set, unsigned 
int first,
 int cfi_probe(struct flash_bank *bank);
 int cfi_auto_probe(struct flash_bank *bank);
 int cfi_protect_check(struct flash_bank *bank);
-int cfi_get_info(struct flash_bank *bank, char *buf, int buf_size);
+int cfi_get_info(struct flash_bank *bank, char *buf, unsigned buf_size);
 int cfi_flash_bank_cmd(struct flash_bank *bank, unsigned int argc, const char 
**argv);
 
 uint32_t cfi_flash_address(struct flash_bank *bank, int sector, uint32_t 
offset);
diff --git a/src/flash/nor/driver.h b/src/flash/nor/driver.h
index e29d4f5..c7cd5fd 100644
--- a/src/flash/nor/driver.h
+++ b/src/flash/nor/driver.h
@@ -209,11 +209,11 @@ struct flash_driver {
         * overflowing the buffer.
         *
         * @param bank - the bank to get info about
-        * @param char - where to put the text for the human to read
-        * @param buf_size - the size of the human buffer.
+        * @param buf - buffer where to put the text for the human to read
+        * @param buf_size - the size of the buffer.
         * @returns ERROR_OK if successful; otherwise, an error code.
         */
-       int (*info)(struct flash_bank *bank, char *buf, int buf_size);
+       int (*info)(struct flash_bank *bank, char *buf, unsigned buf_size);
 
        /**
         * A more gentle flavor of flash_driver_s::probe, performing
diff --git a/src/flash/nor/efm32.c b/src/flash/nor/efm32.c
index 6f29007..552d322 100644
--- a/src/flash/nor/efm32.c
+++ b/src/flash/nor/efm32.c
@@ -327,13 +327,15 @@ static int efm32x_read_info(struct flash_bank *bank,
 /*
  * Helper to create a human friendly string describing a part
  */
-static int efm32x_decode_info(struct efm32_info *info, char *buf, int buf_size)
+static int efm32x_decode_info(struct efm32_info *info, char *buf, unsigned 
buf_size)
 {
        int printed = 0;
        printed = snprintf(buf, buf_size, "%s Gecko, rev %d",
                        info->family_data->name, info->prod_rev);
 
-       if (printed >= buf_size)
+       if (printed < 0)
+               return ERROR_FAIL;
+       if ((unsigned)printed >= buf_size)
                return ERROR_BUF_TOO_SMALL;
 
        return ERROR_OK;
@@ -1044,7 +1046,7 @@ static int efm32x_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int get_efm32x_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_efm32x_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct efm32_info info;
        int ret = 0;
diff --git a/src/flash/nor/esirisc_flash.c b/src/flash/nor/esirisc_flash.c
index 24e8117..199129a 100644
--- a/src/flash/nor/esirisc_flash.c
+++ b/src/flash/nor/esirisc_flash.c
@@ -487,7 +487,7 @@ static int esirisc_flash_auto_probe(struct flash_bank *bank)
        return esirisc_flash_probe(bank);
 }
 
-static int esirisc_flash_info(struct flash_bank *bank, char *buf, int buf_size)
+static int esirisc_flash_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct esirisc_flash_bank *esirisc_info = bank->driver_priv;
 
diff --git a/src/flash/nor/faux.c b/src/flash/nor/faux.c
index d6c6b2d..5ce6436 100644
--- a/src/flash/nor/faux.c
+++ b/src/flash/nor/faux.c
@@ -92,7 +92,7 @@ static int faux_write(struct flash_bank *bank, const uint8_t 
*buffer, uint32_t o
        return ERROR_OK;
 }
 
-static int faux_info(struct flash_bank *bank, char *buf, int buf_size)
+static int faux_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        snprintf(buf, buf_size, "faux flash driver");
        return ERROR_OK;
diff --git a/src/flash/nor/fespi.c b/src/flash/nor/fespi.c
index 02630ac..b32e895 100644
--- a/src/flash/nor/fespi.c
+++ b/src/flash/nor/fespi.c
@@ -1017,7 +1017,7 @@ static int fespi_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int get_fespi_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_fespi_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct fespi_flash_bank *fespi_info = bank->driver_priv;
 
diff --git a/src/flash/nor/fm4.c b/src/flash/nor/fm4.c
index 211a008..a1ba839 100644
--- a/src/flash/nor/fm4.c
+++ b/src/flash/nor/fm4.c
@@ -539,7 +539,7 @@ static int fm4_auto_probe(struct flash_bank *bank)
        return fm4_probe(bank);
 }
 
-static int fm4_get_info_command(struct flash_bank *bank, char *buf, int 
buf_size)
+static int fm4_get_info_command(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct fm4_flash_bank *fm4_bank = bank->driver_priv;
        const char *name;
diff --git a/src/flash/nor/jtagspi.c b/src/flash/nor/jtagspi.c
index 20362f3..38a0052 100644
--- a/src/flash/nor/jtagspi.c
+++ b/src/flash/nor/jtagspi.c
@@ -421,7 +421,7 @@ static int jtagspi_write(struct flash_bank *bank, const 
uint8_t *buffer, uint32_
        return ERROR_OK;
 }
 
-static int jtagspi_info(struct flash_bank *bank, char *buf, int buf_size)
+static int jtagspi_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct jtagspi_flash_bank *info = bank->driver_priv;
 
diff --git a/src/flash/nor/kinetis.c b/src/flash/nor/kinetis.c
index e6b452e..e21d6ec 100644
--- a/src/flash/nor/kinetis.c
+++ b/src/flash/nor/kinetis.c
@@ -2778,7 +2778,7 @@ static int kinetis_auto_probe(struct flash_bank *bank)
        return kinetis_probe(bank);
 }
 
-static int kinetis_info(struct flash_bank *bank, char *buf, int buf_size)
+static int kinetis_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        const char *bank_class_names[] = {
                "(ANY)", "PFlash", "FlexNVM", "FlexRAM"
diff --git a/src/flash/nor/kinetis_ke.c b/src/flash/nor/kinetis_ke.c
index 5aba7fc..3e8efa1 100644
--- a/src/flash/nor/kinetis_ke.c
+++ b/src/flash/nor/kinetis_ke.c
@@ -1171,7 +1171,7 @@ static int kinetis_ke_auto_probe(struct flash_bank *bank)
        return kinetis_ke_probe(bank);
 }
 
-static int kinetis_ke_info(struct flash_bank *bank, char *buf, int buf_size)
+static int kinetis_ke_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        (void) snprintf(buf, buf_size,
                        "%s driver for flash bank %s at " TARGET_ADDR_FMT,
diff --git a/src/flash/nor/lpc2000.c b/src/flash/nor/lpc2000.c
index 3ad62d6..4955487 100644
--- a/src/flash/nor/lpc2000.c
+++ b/src/flash/nor/lpc2000.c
@@ -1552,7 +1552,7 @@ static int lpc2000_erase_check(struct flash_bank *bank)
        return lpc2000_iap_blank_check(bank, 0, bank->num_sectors - 1);
 }
 
-static int get_lpc2000_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_lpc2000_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct lpc2000_flash_bank *lpc2000_info = bank->driver_priv;
 
diff --git a/src/flash/nor/lpcspifi.c b/src/flash/nor/lpcspifi.c
index dd1c63d..a0aceff 100644
--- a/src/flash/nor/lpcspifi.c
+++ b/src/flash/nor/lpcspifi.c
@@ -926,7 +926,7 @@ static int lpcspifi_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int get_lpcspifi_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_lpcspifi_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct lpcspifi_flash_bank *lpcspifi_info = bank->driver_priv;
 
diff --git a/src/flash/nor/max32xxx.c b/src/flash/nor/max32xxx.c
index 586a73b..4d66b6a 100644
--- a/src/flash/nor/max32xxx.c
+++ b/src/flash/nor/max32xxx.c
@@ -113,7 +113,7 @@ FLASH_BANK_COMMAND_HANDLER(max32xxx_flash_bank_command)
        return ERROR_OK;
 }
 
-static int get_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        int printed;
        struct max32xxx_flash_bank *info = bank->driver_priv;
diff --git a/src/flash/nor/mdr.c b/src/flash/nor/mdr.c
index 2518c22..d53b1ae 100644
--- a/src/flash/nor/mdr.c
+++ b/src/flash/nor/mdr.c
@@ -597,7 +597,7 @@ static int mdr_auto_probe(struct flash_bank *bank)
        return mdr_probe(bank);
 }
 
-static int get_mdr_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_mdr_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct mdr_flash_bank *mdr_info = bank->driver_priv;
        snprintf(buf, buf_size, "MDR32Fx - %s",
diff --git a/src/flash/nor/mrvlqspi.c b/src/flash/nor/mrvlqspi.c
index 3f5ce2c..f4ed3bc 100644
--- a/src/flash/nor/mrvlqspi.c
+++ b/src/flash/nor/mrvlqspi.c
@@ -914,7 +914,7 @@ static int mrvlqspi_flash_erase_check(struct flash_bank 
*bank)
        return ERROR_OK;
 }
 
-static int mrvlqspi_get_info(struct flash_bank *bank, char *buf, int buf_size)
+static int mrvlqspi_get_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct mrvlqspi_flash_bank *mrvlqspi_info = bank->driver_priv;
 
diff --git a/src/flash/nor/msp432.c b/src/flash/nor/msp432.c
index b6933e1..26bbf79 100644
--- a/src/flash/nor/msp432.c
+++ b/src/flash/nor/msp432.c
@@ -975,7 +975,7 @@ static int msp432_auto_probe(struct flash_bank *bank)
        return retval;
 }
 
-static int msp432_info(struct flash_bank *bank, char *buf, int buf_size)
+static int msp432_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct msp432_bank *msp432_bank = bank->driver_priv;
        int printed = 0;
@@ -1024,9 +1024,12 @@ static int msp432_info(struct flash_bank *bank, char 
*buf, int buf_size)
                        break;
        }
 
-       buf_size -= printed;
+       if (printed < 0)
+               /* snprintf failed */
+               return ERROR_FAIL;
 
-       if (0 > buf_size)
+       if ((unsigned)printed >= buf_size)
+               /* insufficient buffer */
                return ERROR_BUF_TOO_SMALL;
 
        return ERROR_OK;
diff --git a/src/flash/nor/niietcm4.c b/src/flash/nor/niietcm4.c
index 1831314..e692659 100644
--- a/src/flash/nor/niietcm4.c
+++ b/src/flash/nor/niietcm4.c
@@ -1719,7 +1719,7 @@ static int niietcm4_auto_probe(struct flash_bank *bank)
        return niietcm4_probe(bank);
 }
 
-static int get_niietcm4_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_niietcm4_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct niietcm4_flash_bank *niietcm4_info = bank->driver_priv;
        LOG_INFO("\nNIIET Cortex-M4F %s\n%s", niietcm4_info->chip_name, 
niietcm4_info->chip_brief);
diff --git a/src/flash/nor/nrf5.c b/src/flash/nor/nrf5.c
index 12bbac6..6283ef5 100644
--- a/src/flash/nor/nrf5.c
+++ b/src/flash/nor/nrf5.c
@@ -624,7 +624,7 @@ static const char *nrf5_decode_info_package(uint32_t 
package)
        return "xx";
 }
 
-static int nrf5_info(struct flash_bank *bank, char *buf, int buf_size)
+static int nrf5_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct nrf5_bank *nbank = bank->driver_priv;
        struct nrf5_info *chip = nbank->chip;
@@ -649,7 +649,11 @@ static int nrf5_info(struct flash_bank *bank, char *buf, 
int buf_size)
                                chip->hwid);
        }
        if (res <= 0)
+               /* snprintf failed */
                return ERROR_FAIL;
+       if ((unsigned)res >= buf_size)
+               /* insufficient buffer */
+               return ERROR_BUF_TOO_SMALL;
 
        snprintf(buf + res, buf_size - res, " %ukB Flash, %ukB RAM",
                                chip->flash_size_kb, chip->ram_size_kb);
diff --git a/src/flash/nor/pic32mx.c b/src/flash/nor/pic32mx.c
index c42cfb2..5f71da5 100644
--- a/src/flash/nor/pic32mx.c
+++ b/src/flash/nor/pic32mx.c
@@ -801,13 +801,13 @@ static int pic32mx_auto_probe(struct flash_bank *bank)
        return pic32mx_probe(bank);
 }
 
-static int pic32mx_info(struct flash_bank *bank, char *buf, int buf_size)
+static int pic32mx_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct target *target = bank->target;
        struct mips32_common *mips32 = target->arch_info;
        struct mips_ejtag *ejtag_info = &mips32->ejtag_info;
        uint32_t device_id;
-       int printed = 0, i;
+       int ret;
 
        device_id = ejtag_info->idcode;
 
@@ -819,18 +819,20 @@ static int pic32mx_info(struct flash_bank *bank, char 
*buf, int buf_size)
                return ERROR_FLASH_OPERATION_FAILED;
        }
 
+       int i;
        for (i = 0; pic32mx_devs[i].name != NULL; i++) {
                if (pic32mx_devs[i].devid == (device_id & 0x0fffffff)) {
-                       printed = snprintf(buf, buf_size, "PIC32MX%s", 
pic32mx_devs[i].name);
+                       ret = snprintf(buf, buf_size, "PIC32MX%s", 
pic32mx_devs[i].name);
                        break;
                }
        }
 
        if (pic32mx_devs[i].name == NULL)
-               printed = snprintf(buf, buf_size, "Unknown");
+               ret = snprintf(buf, buf_size, "Unknown");
+
+       buf += snprintf_num_printed(ret, buf_size);
+       buf_size -= snprintf_num_printed(ret, buf_size);
 
-       buf += printed;
-       buf_size -= printed;
        snprintf(buf, buf_size, " Ver: 0x%02x",
                        (unsigned)((device_id >> 28) & 0xf));
 
diff --git a/src/flash/nor/psoc4.c b/src/flash/nor/psoc4.c
index b606e18..45a01cc 100644
--- a/src/flash/nor/psoc4.c
+++ b/src/flash/nor/psoc4.c
@@ -851,7 +851,7 @@ static int psoc4_auto_probe(struct flash_bank *bank)
 }
 
 
-static int get_psoc4_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_psoc4_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct target *target = bank->target;
        struct psoc4_flash_bank *psoc4_info = bank->driver_priv;
@@ -868,27 +868,26 @@ static int get_psoc4_info(struct flash_bank *bank, char 
*buf, int buf_size)
                return ERROR_OK;
        }
 
-       int retval;
-       int printed = 0;
        uint32_t silicon_id;
        uint16_t family_id;
        uint8_t protection;
 
-       retval = psoc4_get_silicon_id(bank, &silicon_id, &family_id, 
&protection);
+       int retval = psoc4_get_silicon_id(bank, &silicon_id, &family_id, 
&protection);
        if (retval != ERROR_OK)
                return retval;
 
+       int snprintf_ret;
        if (family_id != psoc4_info->family_id)
-               printed = snprintf(buf, buf_size, "Family id mismatch 0x%02" 
PRIx16
+               snprintf_ret = snprintf(buf, buf_size, "Family id mismatch 
0x%02" PRIx16
                        "/0x%02" PRIx16 ", silicon id 0x%08" PRIx32,
                        psoc4_info->family_id, family_id, silicon_id);
        else {
-               printed = snprintf(buf, buf_size, "%s silicon id 0x%08" PRIx32 
"",
+               snprintf_ret = snprintf(buf, buf_size, "%s silicon id 0x%08" 
PRIx32 "",
                        family->name, silicon_id);
        }
 
-       buf += printed;
-       buf_size -= printed;
+       buf += snprintf_num_printed(snprintf_ret, buf_size);
+       buf_size -= snprintf_num_printed(snprintf_ret, buf_size);
 
        const char *prot_txt = psoc4_decode_chip_protection(protection);
        snprintf(buf, buf_size, ", flash %" PRIu32 " kb %s", size_in_kb, 
prot_txt);
diff --git a/src/flash/nor/psoc5lp.c b/src/flash/nor/psoc5lp.c
index c651dea..d1fb205 100644
--- a/src/flash/nor/psoc5lp.c
+++ b/src/flash/nor/psoc5lp.c
@@ -753,7 +753,7 @@ static int psoc5lp_nvl_write(struct flash_bank *bank,
 }
 
 static int psoc5lp_nvl_get_info_command(struct flash_bank *bank,
-       char *buf, int buf_size)
+       char *buf, unsigned buf_size)
 {
        struct psoc5lp_nvl_flash_bank *psoc_nvl_bank = bank->driver_priv;
        char part_number[PART_NUMBER_LEN];
@@ -934,7 +934,7 @@ static int psoc5lp_eeprom_write(struct flash_bank *bank,
        return ERROR_OK;
 }
 
-static int psoc5lp_eeprom_get_info_command(struct flash_bank *bank, char *buf, 
int buf_size)
+static int psoc5lp_eeprom_get_info_command(struct flash_bank *bank, char *buf, 
unsigned buf_size)
 {
        struct psoc5lp_eeprom_flash_bank *psoc_eeprom_bank = bank->driver_priv;
        char part_number[PART_NUMBER_LEN];
@@ -1397,7 +1397,7 @@ static int psoc5lp_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int psoc5lp_get_info_command(struct flash_bank *bank, char *buf, int 
buf_size)
+static int psoc5lp_get_info_command(struct flash_bank *bank, char *buf, 
unsigned buf_size)
 {
        struct psoc5lp_flash_bank *psoc_bank = bank->driver_priv;
        char part_number[PART_NUMBER_LEN];
diff --git a/src/flash/nor/psoc6.c b/src/flash/nor/psoc6.c
index 9c834fd..86bd1cd 100644
--- a/src/flash/nor/psoc6.c
+++ b/src/flash/nor/psoc6.c
@@ -495,7 +495,7 @@ static const char *protection_to_str(uint8_t protection)
  * @param buf_size size of the buffer
  * @return ERROR_OK in case of success, ERROR_XXX code otherwise
  
*************************************************************************************************/
-static int psoc6_get_info(struct flash_bank *bank, char *buf, int buf_size)
+static int psoc6_get_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct psoc6_target_info *psoc6_info = bank->driver_priv;
 
diff --git a/src/flash/nor/sh_qspi.c b/src/flash/nor/sh_qspi.c
index 4ec7ebe..fc027f3 100644
--- a/src/flash/nor/sh_qspi.c
+++ b/src/flash/nor/sh_qspi.c
@@ -851,7 +851,7 @@ static int sh_qspi_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int sh_qspi_get_info(struct flash_bank *bank, char *buf, int buf_size)
+static int sh_qspi_get_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct sh_qspi_flash_bank *info = bank->driver_priv;
 
diff --git a/src/flash/nor/sim3x.c b/src/flash/nor/sim3x.c
index 76280f1..f824bd6 100644
--- a/src/flash/nor/sim3x.c
+++ b/src/flash/nor/sim3x.c
@@ -834,41 +834,40 @@ static int sim3x_auto_probe(struct flash_bank *bank)
        }
 }
 
-static int sim3x_flash_info(struct flash_bank *bank, char *buf, int buf_size)
+static int sim3x_flash_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
-       int ret;
-       int printed = 0;
        struct sim3x_info *sim3x_info;
 
        sim3x_info = bank->driver_priv;
 
        /* Read info about chip */
-       ret = sim3x_read_info(bank);
+       int ret = sim3x_read_info(bank);
        if (ret != ERROR_OK)
                return ret;
 
        /* Part */
+       int snprintf_ret;
        if (sim3x_info->part_family && sim3x_info->part_number) {
-               printed = snprintf(buf, buf_size, "SiM3%c%d", 
sim3x_info->part_family, sim3x_info->part_number);
-               buf += printed;
-               buf_size -= printed;
+               snprintf_ret = snprintf(buf, buf_size, "SiM3%c%d", 
sim3x_info->part_family, sim3x_info->part_number);
+               buf += snprintf_num_printed(snprintf_ret, buf_size);
+               buf_size -= snprintf_num_printed(snprintf_ret, buf_size);
 
                if (buf_size <= 0)
                        return ERROR_BUF_TOO_SMALL;
 
                /* Revision */
                if (sim3x_info->device_revision && sim3x_info->device_revision 
<= 'Z' - 'A') {
-                       printed = snprintf(buf, buf_size, "-%c", 
sim3x_info->device_revision + 'A');
-                       buf += printed;
-                       buf_size -= printed;
+                       snprintf_ret = snprintf(buf, buf_size, "-%c", 
sim3x_info->device_revision + 'A');
+                       buf += snprintf_num_printed(snprintf_ret, buf_size);
+                       buf_size -= snprintf_num_printed(snprintf_ret, 
buf_size);
 
                        if (buf_size <= 0)
                                return ERROR_BUF_TOO_SMALL;
 
                        /* Package */
-                       printed = snprintf(buf, buf_size, "-G%s", 
sim3x_info->device_package);
-                       buf += printed;
-                       buf_size -= printed;
+                       snprintf_ret = snprintf(buf, buf_size, "-G%s", 
sim3x_info->device_package);
+                       buf += snprintf_num_printed(snprintf_ret, buf_size);
+                       buf_size -= snprintf_num_printed(snprintf_ret, 
buf_size);
 
                        if (buf_size <= 0)
                                return ERROR_BUF_TOO_SMALL;
@@ -876,10 +875,11 @@ static int sim3x_flash_info(struct flash_bank *bank, char 
*buf, int buf_size)
        }
 
        /* Print flash size */
-       printed = snprintf(buf, buf_size, " flash_size = %dKB", 
sim3x_info->flash_size_kb);
-       buf_size -= printed;
+       snprintf_ret = snprintf(buf, buf_size, " flash_size = %dKB", 
sim3x_info->flash_size_kb);
 
-       if (buf_size <= 0)
+       if (snprintf_ret < 0)
+               return ERROR_FAIL;
+       if ((unsigned)snprintf_ret >= buf_size)
                return ERROR_BUF_TOO_SMALL;
 
        return ERROR_OK;
diff --git a/src/flash/nor/stellaris.c b/src/flash/nor/stellaris.c
index 55b99de..3677c1c 100644
--- a/src/flash/nor/stellaris.c
+++ b/src/flash/nor/stellaris.c
@@ -479,9 +479,8 @@ FLASH_BANK_COMMAND_HANDLER(stellaris_flash_bank_command)
        return ERROR_OK;
 }
 
-static int get_stellaris_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_stellaris_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
-       int printed;
        struct stellaris_flash_bank *stellaris_info = bank->driver_priv;
 
        if (stellaris_info->did1 == 0)
@@ -490,7 +489,8 @@ static int get_stellaris_info(struct flash_bank *bank, char 
*buf, int buf_size)
        /* Read main and master clock frequency register */
        stellaris_read_clock_info(bank);
 
-       printed = snprintf(buf,
+       int snprintf_ret;
+       snprintf_ret = snprintf(buf,
                           buf_size,
                           "\nTI/LMI Stellaris information: Chip is "
                           "class %i (%s) %s rev %c%i\n",
@@ -499,10 +499,11 @@ static int get_stellaris_info(struct flash_bank *bank, 
char *buf, int buf_size)
                           stellaris_info->target_name,
                           (int)('A' + ((stellaris_info->did0 >> 8) & 0xFF)),
                           (int)((stellaris_info->did0) & 0xFF));
-       buf += printed;
-       buf_size -= printed;
 
-       printed = snprintf(buf,
+       buf += snprintf_num_printed(snprintf_ret, buf_size);
+       buf_size -= snprintf_num_printed(snprintf_ret, buf_size);
+
+       snprintf_ret = snprintf(buf,
                           buf_size,
                           "did1: 0x%8.8" PRIx32 ", arch: 0x%4.4" PRIx32
                           ", eproc: %s, ramsize: %" PRIu32 "k, flashsize: %" 
PRIu32 "k\n",
@@ -511,8 +512,9 @@ static int get_stellaris_info(struct flash_bank *bank, char 
*buf, int buf_size)
                           "ARMv7M",
                           stellaris_info->sramsiz,
                           (uint32_t)(stellaris_info->num_pages * 
stellaris_info->pagesize / 1024));
-       buf += printed;
-       buf_size -= printed;
+
+       buf += snprintf_num_printed(snprintf_ret, buf_size);
+       buf_size -= snprintf_num_printed(snprintf_ret, buf_size);
 
        snprintf(buf,
                           buf_size,
diff --git a/src/flash/nor/stm32f1x.c b/src/flash/nor/stm32f1x.c
index 9cd282d..3bb36d8 100644
--- a/src/flash/nor/stm32f1x.c
+++ b/src/flash/nor/stm32f1x.c
@@ -932,7 +932,7 @@ static const char *get_stm32f0_revision(uint16_t rev_id)
        return rev_str;
 }
 
-static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_stm32x_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        uint32_t dbgmcu_idcode;
 
diff --git a/src/flash/nor/stm32f2x.c b/src/flash/nor/stm32f2x.c
index e3625f3..24bf60f 100644
--- a/src/flash/nor/stm32f2x.c
+++ b/src/flash/nor/stm32f2x.c
@@ -1251,7 +1251,7 @@ static int stm32x_auto_probe(struct flash_bank *bank)
        return stm32x_probe(bank);
 }
 
-static int get_stm32x_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_stm32x_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        uint32_t dbgmcu_idcode;
 
diff --git a/src/flash/nor/stm32h7x.c b/src/flash/nor/stm32h7x.c
index 52e3e0e..64ffef0 100644
--- a/src/flash/nor/stm32h7x.c
+++ b/src/flash/nor/stm32h7x.c
@@ -916,7 +916,7 @@ static int stm32x_auto_probe(struct flash_bank *bank)
 }
 
 /* This method must return a string displaying information about the bank */
-static int stm32x_get_info(struct flash_bank *bank, char *buf, int buf_size)
+static int stm32x_get_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct stm32h7x_flash_bank *stm32x_info = bank->driver_priv;
        const struct stm32h7x_part_info *info = stm32x_info->part_info;
diff --git a/src/flash/nor/stm32l4x.c b/src/flash/nor/stm32l4x.c
index 594bce0..ed5a908 100644
--- a/src/flash/nor/stm32l4x.c
+++ b/src/flash/nor/stm32l4x.c
@@ -1608,7 +1608,7 @@ static int stm32l4_auto_probe(struct flash_bank *bank)
        return stm32l4_probe(bank);
 }
 
-static int get_stm32l4_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_stm32l4_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
        const struct stm32l4_part_info *part_info = stm32l4_info->part_info;
@@ -1623,11 +1623,17 @@ static int get_stm32l4_info(struct flash_bank *bank, 
char *buf, int buf_size)
                        }
                }
 
-               int buf_len = snprintf(buf, buf_size, "%s - Rev %s : 0x%04x",
+               int snprintf_ret = snprintf(buf, buf_size, "%s - Rev %s : 
0x%04x",
                                part_info->device_str, rev_str ? rev_str : 
"'unknown'", rev_id);
 
+               if (snprintf_ret < 0)
+                       return ERROR_FAIL;
+               unsigned printed = snprintf_ret;
+               if (printed >= buf_size)
+                       return ERROR_BUF_TOO_SMALL;
+
                if (stm32l4_info->probed)
-                       snprintf(buf + buf_len, buf_size - buf_len, " - 
%s-bank",
+                       snprintf(buf + printed, buf_size - printed, " - 
%s-bank",
                                        stm32l4_is_otp(bank) ? "OTP" :
                                        stm32l4_info->dual_bank_mode ? "Flash 
dual" : "Flash single");
 
diff --git a/src/flash/nor/stm32lx.c b/src/flash/nor/stm32lx.c
index 3cb1a49..fea3777 100644
--- a/src/flash/nor/stm32lx.c
+++ b/src/flash/nor/stm32lx.c
@@ -870,7 +870,7 @@ static int stm32lx_auto_probe(struct flash_bank *bank)
 }
 
 /* This method must return a string displaying information about the bank */
-static int stm32lx_get_info(struct flash_bank *bank, char *buf, int buf_size)
+static int stm32lx_get_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
        const struct stm32lx_part_info *info = &stm32lx_info->part_info;
diff --git a/src/flash/nor/stmqspi.c b/src/flash/nor/stmqspi.c
index 11aa438..0d3311a 100644
--- a/src/flash/nor/stmqspi.c
+++ b/src/flash/nor/stmqspi.c
@@ -2397,7 +2397,7 @@ static int stmqspi_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int get_stmqspi_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_stmqspi_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct stmqspi_flash_bank *stmqspi_info = bank->driver_priv;
 
diff --git a/src/flash/nor/stmsmi.c b/src/flash/nor/stmsmi.c
index f633e36..183a5ca 100644
--- a/src/flash/nor/stmsmi.c
+++ b/src/flash/nor/stmsmi.c
@@ -631,7 +631,7 @@ static int stmsmi_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int get_stmsmi_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_stmsmi_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        struct stmsmi_flash_bank *stmsmi_info = bank->driver_priv;
 
diff --git a/src/flash/nor/str7x.c b/src/flash/nor/str7x.c
index e028c1f..7edabf1 100644
--- a/src/flash/nor/str7x.c
+++ b/src/flash/nor/str7x.c
@@ -702,7 +702,7 @@ COMMAND_HANDLER(str7x_handle_part_id_command)
 }
 #endif
 
-static int get_str7x_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_str7x_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
        /* Setting the write protection on a sector is a permanent change but it
         * can be disabled temporarily. FLASH_NVWPAR reflects the permanent
diff --git a/src/flash/nor/tms470.c b/src/flash/nor/tms470.c
index 3004682..d57ac5a 100644
--- a/src/flash/nor/tms470.c
+++ b/src/flash/nor/tms470.c
@@ -1118,9 +1118,8 @@ static int tms470_protect_check(struct flash_bank *bank)
 
 /* ---------------------------------------------------------------------- */
 
-static int get_tms470_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_tms470_info(struct flash_bank *bank, char *buf, unsigned 
buf_size)
 {
-       int used = 0;
        struct tms470_flash_bank *tms470_info = bank->driver_priv;
 
        if (!tms470_info->device_ident_reg)
@@ -1131,11 +1130,10 @@ static int get_tms470_info(struct flash_bank *bank, 
char *buf, int buf_size)
                return ERROR_FLASH_OPERATION_FAILED;
        }
 
-       used =
-               snprintf(buf, buf_size, "\ntms470 information: Chip is %s\n",
+       int snprintf_ret = snprintf(buf, buf_size, "\ntms470 information: Chip 
is %s\n",
                        tms470_info->part_name);
-       buf += used;
-       buf_size -= used;
+       buf += snprintf_num_printed(snprintf_ret, buf_size);
+       buf_size -= snprintf_num_printed(snprintf_ret, buf_size);
 
        snprintf(buf, buf_size, "Flash protection level 2 is %s\n",
                tms470_check_flash_unlocked(bank->target) == ERROR_OK ? 
"disabled" : "enabled");
diff --git a/src/flash/nor/virtual.c b/src/flash/nor/virtual.c
index 1aa12fe..4d2f731 100644
--- a/src/flash/nor/virtual.c
+++ b/src/flash/nor/virtual.c
@@ -172,7 +172,7 @@ static int virtual_auto_probe(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int virtual_info(struct flash_bank *bank, char *buf, int buf_size)
+static int virtual_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        struct flash_bank *master_bank = virtual_get_master_bank(bank);
 
diff --git a/src/flash/nor/w600.c b/src/flash/nor/w600.c
index 2506f2b..6554ab7 100644
--- a/src/flash/nor/w600.c
+++ b/src/flash/nor/w600.c
@@ -362,7 +362,7 @@ static int w600_auto_probe(struct flash_bank *bank)
        return w600_probe(bank);
 }
 
-static int get_w600_info(struct flash_bank *bank, char *buf, int buf_size)
+static int get_w600_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        uint32_t flash_id;
 
diff --git a/src/flash/nor/xcf.c b/src/flash/nor/xcf.c
index 0cef43b..935c9a2 100644
--- a/src/flash/nor/xcf.c
+++ b/src/flash/nor/xcf.c
@@ -581,7 +581,7 @@ FLASH_BANK_COMMAND_HANDLER(xcf_flash_bank_command)
        return ERROR_OK;
 }
 
-static int xcf_info(struct flash_bank *bank, char *buf, int buf_size)
+static int xcf_info(struct flash_bank *bank, char *buf, unsigned buf_size)
 {
        const struct xcf_priv *priv = bank->driver_priv;
 
diff --git a/src/flash/nor/xmc1xxx.c b/src/flash/nor/xmc1xxx.c
index 757dd95..1f7f48b 100644
--- a/src/flash/nor/xmc1xxx.c
+++ b/src/flash/nor/xmc1xxx.c
@@ -403,7 +403,7 @@ static int xmc1xxx_protect_check(struct flash_bank *bank)
        return ERROR_OK;
 }
 
-static int xmc1xxx_get_info_command(struct flash_bank *bank, char *buf, int 
buf_size)
+static int xmc1xxx_get_info_command(struct flash_bank *bank, char *buf, 
unsigned buf_size)
 {
        uint32_t chipid[8];
        int i, retval;
diff --git a/src/flash/nor/xmc4xxx.c b/src/flash/nor/xmc4xxx.c
index a032e4d..9995dbc 100644
--- a/src/flash/nor/xmc4xxx.c
+++ b/src/flash/nor/xmc4xxx.c
@@ -805,7 +805,7 @@ abort_write_and_exit:
 
 }
 
-static int xmc4xxx_get_info_command(struct flash_bank *bank, char *buf, int 
buf_size)
+static int xmc4xxx_get_info_command(struct flash_bank *bank, char *buf, 
unsigned buf_size)
 {
        struct xmc4xxx_flash_bank *fb = bank->driver_priv;
        uint32_t scu_idcode;
diff --git a/src/helper/log.c b/src/helper/log.c
index 7440b70..17a60df 100644
--- a/src/helper/log.c
+++ b/src/helper/log.c
@@ -517,3 +517,27 @@ void log_socket_error(const char *socket_desc)
        LOG_ERROR("Error on socket '%s': errno==%d, message: %s.", socket_desc, 
error_code, strerror(error_code));
 #endif
 }
+
+/**
+ * Determine the number of characters that snprintf() function really printed 
to the buffer
+ * (excluding the terminating '\0').
+ * @param retval Return value from snprintf()
+ * @param buf_size Buffer size that snprintf() operated on
+ */
+unsigned snprintf_num_printed(int retval, unsigned buf_size)
+{
+       if (retval < 0) {
+               /* snprintf() failed, assume the buffer is untouched */
+               return 0;
+       }
+
+       unsigned printed = retval;
+       if (printed < buf_size) {
+               /* large enough buffer, whole string printed */
+               return printed;
+       } else {
+               /* insufficient buffer, output got truncated */
+               return (buf_size > 0) ? (buf_size - 1) : 0;
+       }
+
+}
diff --git a/src/helper/log.h b/src/helper/log.h
index f2ba0da..9389018 100644
--- a/src/helper/log.h
+++ b/src/helper/log.h
@@ -84,6 +84,8 @@ void busy_sleep(uint64_t ms);
 
 void log_socket_error(const char *socket_desc);
 
+unsigned snprintf_num_printed(int retval, unsigned buf_size);
+
 typedef void (*log_callback_fn)(void *priv, const char *file, unsigned line,
                const char *function, const char *string);
 

-- 

Reply via email to