Skip to content

Commit 500e37f

Browse files
committed
mbed_error: Always print full filename if passed
Don't extract filename from the stored error - print it directly. Use "mbed_error_puts" for both error message and filename to avoid buffer length limits. Switch to puts also fixes the potential problem of an error message containing a '%' upsetting the formatter - it should have been mbed_printf_error("%s", error_msg) in the first place.
1 parent 10fe322 commit 500e37f

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

platform/mbed_error.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
#endif
3131

3232
#ifndef NDEBUG
33-
#define ERROR_REPORT(ctx, error_msg) print_error_report(ctx, error_msg)
33+
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line) print_error_report(ctx, error_msg, error_filename, error_line)
34+
static void print_error_report(const mbed_error_ctx *ctx, const char *, const char *error_filename, int error_line);
3435
#else
35-
#define ERROR_REPORT(ctx, error_msg) ((void) 0)
36+
#define ERROR_REPORT(ctx, error_msg, error_filename, error_line) ((void) 0)
3637
#endif
3738

3839
static uint8_t error_in_progress = 0;
3940
static int error_count = 0;
4041
static mbed_error_ctx first_error_ctx = {0};
4142
static mbed_error_ctx last_error_ctx = {0};
4243
static mbed_error_hook_t error_hook = NULL;
43-
static void print_error_report(mbed_error_ctx *ctx, const char *);
4444
static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsigned int error_value, const char *filename, int line_number, void *caller);
4545

4646
//Helper function to halt the system
@@ -67,7 +67,7 @@ WEAK void error(const char *format, ...)
6767

6868
//Call handle_error/print_error_report permanently setting error_in_progress flag
6969
handle_error(MBED_ERROR_UNKNOWN, 0, NULL, 0, MBED_CALLER_ADDR());
70-
ERROR_REPORT(&last_error_ctx, "Fatal Run-time error");
70+
ERROR_REPORT(&last_error_ctx, "Fatal Run-time error", NULL, 0);
7171
error_in_progress = 1;
7272

7373
#ifndef NDEBUG
@@ -185,7 +185,7 @@ WEAK mbed_error_status_t mbed_error(mbed_error_status_t error_status, const char
185185
}
186186

187187
//On fatal errors print the error context/report
188-
ERROR_REPORT(&last_error_ctx, error_msg);
188+
ERROR_REPORT(&last_error_ctx, error_msg, filename, line_number);
189189
mbed_halt_system();
190190

191191
return MBED_ERROR_FAILED_OPERATION;
@@ -293,7 +293,7 @@ static void print_threads_info(const osRtxThread_t *threads)
293293
#endif
294294

295295
#ifndef NDEBUG
296-
static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
296+
static void print_error_report(const mbed_error_ctx *ctx, const char *error_msg, const char *error_filename, int error_line)
297297
{
298298
uint32_t error_code = MBED_GET_ERROR_CODE(ctx->error_status);
299299
uint32_t error_module = MBED_GET_ERROR_MODULE(ctx->error_status);
@@ -342,15 +342,21 @@ static void print_error_report(mbed_error_ctx *ctx, const char *error_msg)
342342
//Nothing to do here, just print the error info down
343343
break;
344344
}
345-
mbed_error_printf(error_msg);
345+
mbed_error_puts(error_msg);
346346
mbed_error_printf("\nLocation: 0x%X", ctx->error_address);
347347

348-
#if MBED_CONF_PLATFORM_ERROR_FILENAME_CAPTURE_ENABLED && !defined(NDEBUG)
349-
if ((NULL != ctx->error_filename[0]) && (ctx->error_line_number != 0)) {
350-
//for string, we must pass address of a ptr which has the address of the string
351-
mbed_error_printf("\nFile:%s+%d", ctx->error_filename, ctx->error_line_number);
348+
/* We print the filename passed in, not any filename in the context. This
349+
* avoids the console print for mbed_error being limited to the presence
350+
* and length of the filename storage. Note that although the MBED_ERROR
351+
* macro compiles out filenames unless platform.error-filename-capture-enabled
352+
* is turned on, MBED_ASSERT always passes filenames, and other direct
353+
* users of mbed_error() may also choose to.
354+
*/
355+
if (error_filename) {
356+
mbed_error_puts("\nFile: ");
357+
mbed_error_puts(error_filename);
358+
mbed_error_printf("+%d", error_line);
352359
}
353-
#endif
354360

355361
mbed_error_printf("\nError Value: 0x%X", ctx->error_value);
356362
#ifdef MBED_CONF_RTOS_PRESENT

0 commit comments

Comments
 (0)