Skip to content

Commit 430fb3e

Browse files
author
Cruz Monrreal
authored
Merge pull request #8601 from kjbracey-arm/error_fmtcheck
Add format checking to printf-type APIs
2 parents 9c5fc7d + 3da44f6 commit 430fb3e

File tree

11 files changed

+88
-69
lines changed

11 files changed

+88
-69
lines changed

cmsis/TARGET_CORTEX_M/mbed_fault_handler.c

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
* limitations under the License.
1515
*/
1616

17+
#ifndef __STDC_FORMAT_MACROS
18+
#define __STDC_FORMAT_MACROS
19+
#endif
20+
#include <inttypes.h>
21+
1722
#include "device.h"
1823
#include "platform/mbed_error.h"
1924
#include "platform/mbed_interface.h"
@@ -72,37 +77,37 @@ MBED_NOINLINE void print_context_info(void)
7277
{
7378
//Context Regs
7479
for(int i=0;i<13;i++) {
75-
mbed_error_printf("\nR%-4d: %08X", i, ((uint32_t *)&mbed_fault_context)[i]);
80+
mbed_error_printf("\nR%-4d: %08" PRIX32, i, ((uint32_t *)&mbed_fault_context)[i]);
7681
}
7782

78-
mbed_error_printf("\nSP : %08X"
79-
"\nLR : %08X"
80-
"\nPC : %08X"
81-
"\nxPSR : %08X"
82-
"\nPSP : %08X"
83-
"\nMSP : %08X", mbed_fault_context.SP_reg, mbed_fault_context.LR_reg, mbed_fault_context.PC_reg,
83+
mbed_error_printf("\nSP : %08" PRIX32
84+
"\nLR : %08" PRIX32
85+
"\nPC : %08" PRIX32
86+
"\nxPSR : %08" PRIX32
87+
"\nPSP : %08" PRIX32
88+
"\nMSP : %08" PRIX32, mbed_fault_context.SP_reg, mbed_fault_context.LR_reg, mbed_fault_context.PC_reg,
8489
mbed_fault_context.xPSR, mbed_fault_context.PSP, mbed_fault_context.MSP );
8590

8691
//Capture CPUID to get core/cpu info
87-
mbed_error_printf("\nCPUID: %08X", SCB->CPUID);
92+
mbed_error_printf("\nCPUID: %08" PRIX32, SCB->CPUID);
8893

8994
#if !defined(TARGET_M0) && !defined(TARGET_M0P)
9095
//Capture fault information registers to infer the cause of exception
91-
mbed_error_printf("\nHFSR : %08X"
92-
"\nMMFSR: %08X"
93-
"\nBFSR : %08X"
94-
"\nUFSR : %08X"
95-
"\nDFSR : %08X"
96-
"\nAFSR : %08X" ////Split/Capture CFSR into MMFSR, BFSR, UFSR
96+
mbed_error_printf("\nHFSR : %08" PRIX32
97+
"\nMMFSR: %08" PRIX32
98+
"\nBFSR : %08" PRIX32
99+
"\nUFSR : %08" PRIX32
100+
"\nDFSR : %08" PRIX32
101+
"\nAFSR : %08" PRIX32 ////Split/Capture CFSR into MMFSR, BFSR, UFSR
97102
,SCB->HFSR, (0xFF & SCB->CFSR), ((0xFF00 & SCB->CFSR) >> 8), ((0xFFFF0000 & SCB->CFSR) >> 16), SCB->DFSR, SCB->AFSR );
98103

99104
//Print MMFAR only if its valid as indicated by MMFSR
100105
if ((0xFF & SCB->CFSR) & 0x80) {
101-
mbed_error_printf("\nMMFAR: %08X",SCB->MMFAR);
106+
mbed_error_printf("\nMMFAR: %08" PRIX32, SCB->MMFAR);
102107
}
103108
//Print BFAR only if its valid as indicated by BFSR
104109
if (((0xFF00 & SCB->CFSR) >> 8) & 0x80) {
105-
mbed_error_printf("\nBFAR : %08X",SCB->BFAR);
110+
mbed_error_printf("\nBFAR : %08" PRIX32, SCB->BFAR);
106111
}
107112
#endif
108113

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@
141141
#include "SDBlockDevice.h"
142142
#include "platform/mbed_debug.h"
143143
#include "platform/mbed_wait_api.h"
144+
#ifndef __STDC_FORMAT_MACROS
145+
#define __STDC_FORMAT_MACROS
146+
#endif
147+
#include <inttypes.h>
144148
#include <errno.h>
145149

146150
#ifndef MBED_CONF_SD_CMD_TIMEOUT
@@ -240,6 +244,9 @@
240244
#define SPI_READ_ERROR_ECC_C (0x1 << 2) /*!< Card ECC failed */
241245
#define SPI_READ_ERROR_OFR (0x1 << 3) /*!< Out of Range */
242246

247+
// Only HC block size is supported. Making this a static constant reduces code size.
248+
const uint32_t SDBlockDevice::_block_size = BLOCK_SIZE_HC;
249+
243250
SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz, bool crc_on)
244251
: _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0),
245252
_crc_on(crc_on), _init_ref_count(0), _crc16(0, 0, false, false)
@@ -253,8 +260,6 @@ SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName c
253260
_init_sck = MBED_CONF_SD_INIT_FREQUENCY;
254261
_transfer_sck = hz;
255262

256-
// Only HC block size is supported.
257-
_block_size = BLOCK_SIZE_HC;
258263
_erase_size = BLOCK_SIZE_HC;
259264
}
260265

@@ -386,7 +391,7 @@ int SDBlockDevice::init()
386391

387392
// Set block length to 512 (CMD16)
388393
if (_cmd(CMD16_SET_BLOCKLEN, _block_size) != 0) {
389-
debug_if(SD_DBG, "Set %d-byte block timed out\n", _block_size);
394+
debug_if(SD_DBG, "Set %" PRIu32 "-byte block timed out\n", _block_size);
390395
unlock();
391396
return BD_ERROR_DEVICE_ERROR;
392397
}
@@ -444,7 +449,7 @@ int SDBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
444449
uint8_t response;
445450

446451
// Get block count
447-
bd_addr_t blockCnt = size / _block_size;
452+
size_t blockCnt = size / _block_size;
448453

449454
// SDSC Card (CCS=0) uses byte unit address
450455
// SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit)
@@ -514,7 +519,7 @@ int SDBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
514519

515520
uint8_t *buffer = static_cast<uint8_t *>(b);
516521
int status = BD_ERROR_OK;
517-
bd_addr_t blockCnt = size / _block_size;
522+
size_t blockCnt = size / _block_size;
518523

519524
// SDSC Card (CCS=0) uses byte unit address
520525
// SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit)
@@ -738,24 +743,24 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
738743
// Process the response R1 : Exit on CRC/Illegal command error/No response
739744
if (R1_NO_RESPONSE == response) {
740745
_deselect();
741-
debug_if(SD_DBG, "No response CMD:%d response: 0x%x\n", cmd, response);
746+
debug_if(SD_DBG, "No response CMD:%d response: 0x%" PRIx32 "\n", cmd, response);
742747
return SD_BLOCK_DEVICE_ERROR_NO_DEVICE; // No device
743748
}
744749
if (response & R1_COM_CRC_ERROR) {
745750
_deselect();
746-
debug_if(SD_DBG, "CRC error CMD:%d response 0x%x \n", cmd, response);
751+
debug_if(SD_DBG, "CRC error CMD:%d response 0x%" PRIx32 "\n", cmd, response);
747752
return SD_BLOCK_DEVICE_ERROR_CRC; // CRC error
748753
}
749754
if (response & R1_ILLEGAL_COMMAND) {
750755
_deselect();
751-
debug_if(SD_DBG, "Illegal command CMD:%d response 0x%x\n", cmd, response);
756+
debug_if(SD_DBG, "Illegal command CMD:%d response 0x%" PRIx32 "\n", cmd, response);
752757
if (CMD8_SEND_IF_COND == cmd) { // Illegal command is for Ver1 or not SD Card
753758
_card_type = CARD_UNKNOWN;
754759
}
755760
return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED; // Command not supported
756761
}
757762

758-
debug_if(_dbg, "CMD:%d \t arg:0x%x \t Response:0x%x \n", cmd, arg, response);
763+
debug_if(_dbg, "CMD:%d \t arg:0x%" PRIx32 " \t Response:0x%" PRIx32 "\n", cmd, arg, response);
759764
// Set status for other errors
760765
if ((response & R1_ERASE_RESET) || (response & R1_ERASE_SEQUENCE_ERROR)) {
761766
status = SD_BLOCK_DEVICE_ERROR_ERASE; // Erase error
@@ -775,7 +780,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
775780
response |= (_spi.write(SPI_FILL_CHAR) << 16);
776781
response |= (_spi.write(SPI_FILL_CHAR) << 8);
777782
response |= _spi.write(SPI_FILL_CHAR);
778-
debug_if(_dbg, "R3/R7: 0x%x \n", response);
783+
debug_if(_dbg, "R3/R7: 0x%" PRIx32 "\n", response);
779784
break;
780785

781786
case CMD12_STOP_TRANSMISSION: // Response R1b
@@ -785,7 +790,7 @@ int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAc
785790

786791
case ACMD13_SD_STATUS: // Response R2
787792
response = _spi.write(SPI_FILL_CHAR);
788-
debug_if(_dbg, "R2: 0x%x \n", response);
793+
debug_if(_dbg, "R2: 0x%" PRIx32 "\n", response);
789794
break;
790795

791796
default: // Response R1
@@ -822,7 +827,7 @@ int SDBlockDevice::_cmd8()
822827
if ((BD_ERROR_OK == status) && (SDCARD_V2 == _card_type)) {
823828
// If check pattern is not matched, CMD8 communication is not valid
824829
if ((response & 0xFFF) != arg) {
825-
debug_if(SD_DBG, "CMD8 Pattern mismatch 0x%x : 0x%x\n", arg, response);
830+
debug_if(SD_DBG, "CMD8 Pattern mismatch 0x%" PRIx32 " : 0x%" PRIx32 "\n", arg, response);
826831
_card_type = CARD_UNKNOWN;
827832
status = SD_BLOCK_DEVICE_ERROR_UNUSABLE;
828833
}
@@ -874,8 +879,8 @@ int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length)
874879
// Compute and verify checksum
875880
_crc16.compute((void *)buffer, length, &crc_result);
876881
if ((uint16_t)crc_result != crc) {
877-
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%x result of computation 0x%x\n",
878-
crc, crc_result);
882+
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n",
883+
crc, (uint16_t)crc_result);
879884
_deselect();
880885
return SD_BLOCK_DEVICE_ERROR_CRC;
881886
}
@@ -908,8 +913,8 @@ int SDBlockDevice::_read(uint8_t *buffer, uint32_t length)
908913
// Compute and verify checksum
909914
_crc16.compute((void *)buffer, length, &crc_result);
910915
if ((uint16_t)crc_result != crc) {
911-
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%x result of computation 0x%x\n",
912-
crc, crc_result);
916+
debug_if(SD_DBG, "_read_bytes: Invalid CRC received 0x%" PRIx16 " result of computation 0x%" PRIx16 "\n",
917+
crc, (uint16_t)crc_result);
913918
return SD_BLOCK_DEVICE_ERROR_CRC;
914919
}
915920
}
@@ -992,11 +997,11 @@ bd_size_t SDBlockDevice::_sd_sectors()
992997
block_len = 1 << read_bl_len; // BLOCK_LEN = 2^READ_BL_LEN
993998
mult = 1 << (c_size_mult + 2); // MULT = 2^C_SIZE_MULT+2 (C_SIZE_MULT < 8)
994999
blocknr = (c_size + 1) * mult; // BLOCKNR = (C_SIZE+1) * MULT
995-
capacity = blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN
1000+
capacity = (bd_size_t) blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN
9961001
blocks = capacity / _block_size;
997-
debug_if(SD_DBG, "Standard Capacity: c_size: %d \n", c_size);
998-
debug_if(SD_DBG, "Sectors: 0x%x : %llu\n", blocks, blocks);
999-
debug_if(SD_DBG, "Capacity: 0x%x : %llu MB\n", capacity, (capacity / (1024U * 1024U)));
1002+
debug_if(SD_DBG, "Standard Capacity: c_size: %" PRIu32 " \n", c_size);
1003+
debug_if(SD_DBG, "Sectors: 0x%" PRIx64 " : %" PRIu64 "\n", blocks, blocks);
1004+
debug_if(SD_DBG, "Capacity: 0x%" PRIx64 " : %" PRIu64 " MB\n", capacity, (capacity / (1024U * 1024U)));
10001005

10011006
// ERASE_BLK_EN = 1: Erase in multiple of 512 bytes supported
10021007
if (ext_bits(csd, 46, 46)) {
@@ -1010,9 +1015,9 @@ bd_size_t SDBlockDevice::_sd_sectors()
10101015
case 1:
10111016
hc_c_size = ext_bits(csd, 69, 48); // device size : C_SIZE : [69:48]
10121017
blocks = (hc_c_size + 1) << 10; // block count = C_SIZE+1) * 1K byte (512B is block size)
1013-
debug_if(SD_DBG, "SDHC/SDXC Card: hc_c_size: %d \n", hc_c_size);
1014-
debug_if(SD_DBG, "Sectors: 0x%x : %llu\n", blocks, blocks);
1015-
debug_if(SD_DBG, "Capacity: %llu MB\n", (blocks / (2048U)));
1018+
debug_if(SD_DBG, "SDHC/SDXC Card: hc_c_size: %" PRIu32 " \n", hc_c_size);
1019+
debug_if(SD_DBG, "Sectors: 0x%" PRIx64 "x : %" PRIu64 "\n", blocks, blocks);
1020+
debug_if(SD_DBG, "Capacity: %" PRIu64 " MB\n", (blocks / (2048U)));
10161021
// ERASE_BLK_EN is fixed to 1, which means host can erase one or multiple of 512 bytes.
10171022
_erase_size = BLOCK_SIZE_HC;
10181023
break;

components/storage/blockdevice/COMPONENT_SD/SDBlockDevice.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ class SDBlockDevice : public BlockDevice {
214214
}
215215

216216
PlatformMutex _mutex;
217-
bd_size_t _block_size;
218-
bd_size_t _erase_size;
217+
static const uint32_t _block_size;
218+
uint32_t _erase_size;
219219
bool _is_initialized;
220220
bool _dbg;
221221
bool _crc_on;

drivers/RawSerial.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY)
2222

23+
#include "mbed_toolchain.h"
2324
#include "drivers/SerialBase.h"
2425
#include "hal/serial_api.h"
2526
#include "platform/NonCopyable.h"
@@ -86,7 +87,7 @@ class RawSerial: public SerialBase, private NonCopyable<RawSerial> {
8687
*/
8788
int puts(const char *str);
8889

89-
int printf(const char *format, ...);
90+
int printf(const char *format, ...) MBED_PRINTF_METHOD(1, 2);
9091

9192
#if !(DOXYGEN_ONLY)
9293
protected:

features/storage/filesystem/fat/FATFileSystem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ DSTATUS disk_initialize(BYTE pdrv)
205205

206206
DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
207207
{
208-
debug_if(FFS_DBG, "disk_read(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
208+
debug_if(FFS_DBG, "disk_read(sector %lu, count %u) on pdrv [%d]\n", sector, count, pdrv);
209209
DWORD ssize = disk_get_sector_size(pdrv);
210210
bd_addr_t addr = (bd_addr_t)sector*ssize;
211211
bd_size_t size = (bd_size_t)count*ssize;
@@ -215,7 +215,7 @@ DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
215215

216216
DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
217217
{
218-
debug_if(FFS_DBG, "disk_write(sector %d, count %d) on pdrv [%d]\n", sector, count, pdrv);
218+
debug_if(FFS_DBG, "disk_write(sector %lu, count %u) on pdrv [%d]\n", sector, count, pdrv);
219219
DWORD ssize = disk_get_sector_size(pdrv);
220220
bd_addr_t addr = (bd_addr_t)sector*ssize;
221221
bd_size_t size = (bd_size_t)count*ssize;
@@ -572,7 +572,7 @@ void FATFileSystem::unlock()
572572
////// File operations //////
573573
int FATFileSystem::file_open(fs_file_t *file, const char *path, int flags)
574574
{
575-
debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%s]\n", path, getName(), _id);
575+
debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", path, getName(), _id);
576576

577577
FIL *fh = new FIL;
578578
Deferred<const char*> fpath = fat_path_prefix(_id, path);

platform/Stream.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "platform/FileLike.h"
2121
#include "platform/FileHandle.h"
2222
#include "platform/NonCopyable.h"
23+
#include "mbed_toolchain.h"
2324
#include <cstdio>
2425
#include <cstdarg>
2526

@@ -47,10 +48,10 @@ class Stream : public FileLike, private NonCopyable<Stream> {
4748
int puts(const char *s);
4849
int getc();
4950
char *gets(char *s, int size);
50-
int printf(const char *format, ...);
51-
int scanf(const char *format, ...);
52-
int vprintf(const char *format, std::va_list args);
53-
int vscanf(const char *format, std::va_list args);
51+
int printf(const char *format, ...) MBED_PRINTF_METHOD(1, 2);
52+
int scanf(const char *format, ...) MBED_SCANF_METHOD(1, 2);
53+
int vprintf(const char *format, std::va_list args) MBED_PRINTF_METHOD(1, 0);
54+
int vscanf(const char *format, std::va_list args) MBED_SCANF_METHOD(1, 0);
5455

5556
operator std::FILE *()
5657
{

platform/mbed_debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
#include <stdio.h>
2828
#include <stdarg.h>
2929
#endif
30+
#include "mbed_toolchain.h"
3031

3132
#ifdef __cplusplus
3233
extern "C" {
3334
#endif
3435

36+
static inline void debug(const char *format, ...) MBED_PRINTF(1, 2);
37+
static inline void debug_if(int condition, const char *format, ...) MBED_PRINTF(2, 3);
3538

3639
/** Output a debug message
3740
*

0 commit comments

Comments
 (0)