From 4611ba4f3a089039d6934380acf64b899611fd2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Tue, 28 Jan 2025 09:02:02 +0100 Subject: [PATCH] Add -Wformat=2 compiler flag --- CMakeLists.txt | 2 +- libbf.c | 14 ++++++-------- quickjs-libc.c | 14 ++++++++------ quickjs.c | 44 +++++++++++++++++--------------------------- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8683dcf35..ce950d65f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,13 +31,13 @@ if(NOT MSVC AND NOT IOS) xcheck_add_c_compiler_flag(-Werror) xcheck_add_c_compiler_flag(-Wextra) endif() +xcheck_add_c_compiler_flag(-Wformat=2) xcheck_add_c_compiler_flag(-Wno-implicit-fallthrough) xcheck_add_c_compiler_flag(-Wno-sign-compare) xcheck_add_c_compiler_flag(-Wno-missing-field-initializers) xcheck_add_c_compiler_flag(-Wno-unused-parameter) xcheck_add_c_compiler_flag(-Wno-unused-but-set-variable) xcheck_add_c_compiler_flag(-Wno-array-bounds) -xcheck_add_c_compiler_flag(-Wno-format-truncation) xcheck_add_c_compiler_flag(-funsigned-char) # ClangCL is command line compatible with MSVC, so 'MSVC' is set. diff --git a/libbf.c b/libbf.c index 07d27cfa1..26872c6df 100644 --- a/libbf.c +++ b/libbf.c @@ -3962,22 +3962,20 @@ static char *bf_ftoa_internal(size_t *plen, const bf_t *a2, int radix, n = 1; if ((flags & BF_FTOA_FORCE_EXP) || n <= -6 || n > n_max) { - const char *fmt; /* exponential notation */ output_digits(s, a1, radix, n_digits, 1, is_dec); if (radix_bits != 0 && radix <= 16) { + slimb_t exp_n = (n - 1) * radix_bits; if (flags & BF_FTOA_JS_QUIRKS) - fmt = "p%+" PRId_LIMB; + dbuf_printf(s, "p%+" PRId_LIMB, exp_n); else - fmt = "p%" PRId_LIMB; - dbuf_printf(s, fmt, (n - 1) * radix_bits); + dbuf_printf(s, "p%" PRId_LIMB, exp_n); } else { + const char c = radix <= 10 ? 'e' : '@'; if (flags & BF_FTOA_JS_QUIRKS) - fmt = "%c%+" PRId_LIMB; + dbuf_printf(s, "%c%+" PRId_LIMB, c, n - 1); else - fmt = "%c%" PRId_LIMB; - dbuf_printf(s, fmt, - radix <= 10 ? 'e' : '@', n - 1); + dbuf_printf(s, "%c%" PRId_LIMB, c, n - 1); } } else if (n <= 0) { /* 0.x */ diff --git a/quickjs-libc.c b/quickjs-libc.c index 0222b2e57..992092641 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -190,6 +190,8 @@ static void js_set_thread_state(JSRuntime *rt, JSThreadState *ts) js_std_cmd(/*SetOpaque*/1, rt, ts); } +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" static JSValue js_printf_internal(JSContext *ctx, int argc, JSValue *argv, FILE *fp) { @@ -207,7 +209,6 @@ static JSValue js_printf_internal(JSContext *ctx, int64_t int64_arg; double double_arg; const char *string_arg; - int (*dbuf_printf_fun)(DynBuf *s, const char *fmt, ...) = dbuf_printf; js_std_dbuf_init(ctx, &dbuf); @@ -332,17 +333,17 @@ static JSValue js_printf_internal(JSContext *ctx, q[0] = '6'; q[1] = '4'; q[3] = '\0'; - dbuf_printf_fun(&dbuf, fmtbuf, (int64_t)int64_arg); + dbuf_printf(&dbuf, fmtbuf, (int64_t)int64_arg); #else if (q >= fmtbuf + sizeof(fmtbuf) - 2) goto invalid; q[1] = q[-1]; q[-1] = q[0] = 'l'; q[2] = '\0'; - dbuf_printf_fun(&dbuf, fmtbuf, (long long)int64_arg); + dbuf_printf(&dbuf, fmtbuf, (long long)int64_arg); #endif } else { - dbuf_printf_fun(&dbuf, fmtbuf, (int)int64_arg); + dbuf_printf(&dbuf, fmtbuf, (int)int64_arg); } break; @@ -353,7 +354,7 @@ static JSValue js_printf_internal(JSContext *ctx, string_arg = JS_ToCString(ctx, argv[i++]); if (!string_arg) goto fail; - dbuf_printf_fun(&dbuf, fmtbuf, string_arg); + dbuf_printf(&dbuf, fmtbuf, string_arg); JS_FreeCString(ctx, string_arg); break; @@ -369,7 +370,7 @@ static JSValue js_printf_internal(JSContext *ctx, goto missing; if (JS_ToFloat64(ctx, &double_arg, argv[i++])) goto fail; - dbuf_printf_fun(&dbuf, fmtbuf, double_arg); + dbuf_printf(&dbuf, fmtbuf, double_arg); break; case '%': @@ -406,6 +407,7 @@ static JSValue js_printf_internal(JSContext *ctx, dbuf_free(&dbuf); return JS_EXCEPTION; } +#pragma GCC diagnostic pop // ignored "-Wformat-nonliteral" uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename) { diff --git a/quickjs.c b/quickjs.c index a90547e10..84bf5c4dc 100644 --- a/quickjs.c +++ b/quickjs.c @@ -3197,7 +3197,6 @@ JSValue JS_NewSymbol(JSContext *ctx, const char *description, bool is_global) #define ATOM_GET_STR_BUF_SIZE 64 -/* Should only be used for debug. */ static const char *JS_AtomGetStrRT(JSRuntime *rt, char *buf, int buf_size, JSAtom atom) { @@ -3220,13 +3219,6 @@ static const char *JS_AtomGetStrRT(JSRuntime *rt, char *buf, int buf_size, /* encode surrogates correctly */ utf8_encode_buf16(buf, buf_size, str->u.str16, str->len); } else { - /* special case ASCII strings */ - int i, c = 0; - for(i = 0; i < str->len; i++) { - c |= str->u.str8[i]; - } - if (c < 0x80) - return (const char *)str->u.str8; utf8_encode_buf8(buf, buf_size, str->u.str8, str->len); } } @@ -6856,8 +6848,9 @@ static JSValue JS_MakeError(JSContext *ctx, JSErrorEnum error_num, } /* fmt and arguments may be pure ASCII or UTF-8 encoded contents */ -static JSValue JS_ThrowError2(JSContext *ctx, JSErrorEnum error_num, - const char *fmt, va_list ap, bool add_backtrace) +static JSValue JS_PRINTF_FORMAT_ATTR(4, 0) +JS_ThrowError2(JSContext *ctx, JSErrorEnum error_num, + bool add_backtrace, JS_PRINTF_FORMAT const char *fmt, va_list ap) { char buf[256]; JSValue obj; @@ -6871,8 +6864,9 @@ static JSValue JS_ThrowError2(JSContext *ctx, JSErrorEnum error_num, return JS_Throw(ctx, obj); } -static JSValue JS_ThrowError(JSContext *ctx, JSErrorEnum error_num, - const char *fmt, va_list ap) +static JSValue JS_PRINTF_FORMAT_ATTR(3, 0) +JS_ThrowError(JSContext *ctx, JSErrorEnum error_num, + JS_PRINTF_FORMAT const char *fmt, va_list ap) { JSRuntime *rt = ctx->rt; JSStackFrame *sf; @@ -6882,7 +6876,7 @@ static JSValue JS_ThrowError(JSContext *ctx, JSErrorEnum error_num, sf = rt->current_stack_frame; add_backtrace = !rt->in_out_of_memory && (!sf || (JS_GetFunctionBytecode(sf->cur_func) == NULL)); - return JS_ThrowError2(ctx, error_num, fmt, ap, add_backtrace); + return JS_ThrowError2(ctx, error_num, add_backtrace, fmt, ap); } JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...) @@ -6933,26 +6927,22 @@ static int JS_PRINTF_FORMAT_ATTR(3, 4) JS_ThrowTypeErrorOrFalse(JSContext *ctx, } } -/* never use it directly */ -static JSValue JS_PRINTF_FORMAT_ATTR(3, 4) __JS_ThrowTypeErrorAtom(JSContext *ctx, JSAtom atom, JS_PRINTF_FORMAT const char *fmt, ...) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wformat-nonliteral" +static JSValue JS_ThrowTypeErrorAtom(JSContext *ctx, const char *fmt, JSAtom atom) { char buf[ATOM_GET_STR_BUF_SIZE]; - return JS_ThrowTypeError(ctx, fmt, - JS_AtomGetStr(ctx, buf, sizeof(buf), atom)); + JS_AtomGetStr(ctx, buf, sizeof(buf), atom); + return JS_ThrowTypeError(ctx, fmt, buf); } -/* never use it directly */ -static JSValue JS_PRINTF_FORMAT_ATTR(3, 4) __JS_ThrowSyntaxErrorAtom(JSContext *ctx, JSAtom atom, JS_PRINTF_FORMAT const char *fmt, ...) +static JSValue JS_ThrowSyntaxErrorAtom(JSContext *ctx, const char *fmt, JSAtom atom) { char buf[ATOM_GET_STR_BUF_SIZE]; - return JS_ThrowSyntaxError(ctx, fmt, - JS_AtomGetStr(ctx, buf, sizeof(buf), atom)); + JS_AtomGetStr(ctx, buf, sizeof(buf), atom); + return JS_ThrowSyntaxError(ctx, fmt, buf); } - -/* %s is replaced by 'atom'. The macro is used so that gcc can check - the format string. */ -#define JS_ThrowTypeErrorAtom(ctx, fmt, atom) __JS_ThrowTypeErrorAtom(ctx, atom, fmt, "") -#define JS_ThrowSyntaxErrorAtom(ctx, fmt, atom) __JS_ThrowSyntaxErrorAtom(ctx, atom, fmt, "") +#pragma GCC diagnostic pop // ignored "-Wformat-nonliteral" static int JS_ThrowTypeErrorReadOnly(JSContext *ctx, int flags, JSAtom atom) { @@ -19038,7 +19028,7 @@ int JS_PRINTF_FORMAT_ATTR(2, 3) js_parse_error(JSParseState *s, JS_PRINTF_FORMAT int backtrace_flags; va_start(ap, fmt); - JS_ThrowError2(ctx, JS_SYNTAX_ERROR, fmt, ap, false); + JS_ThrowError2(ctx, JS_SYNTAX_ERROR, false, fmt, ap); va_end(ap); backtrace_flags = 0; if (s->cur_func && s->cur_func->backtrace_barrier)