Skip to content

Commit d162c06

Browse files
committed
Add -Wformat=2 compiler flag
1 parent f316533 commit d162c06

File tree

4 files changed

+32
-42
lines changed

4 files changed

+32
-42
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ if(NOT MSVC AND NOT IOS)
3131
xcheck_add_c_compiler_flag(-Werror)
3232
xcheck_add_c_compiler_flag(-Wextra)
3333
endif()
34+
xcheck_add_c_compiler_flag(-Wformat=2)
3435
xcheck_add_c_compiler_flag(-Wno-implicit-fallthrough)
3536
xcheck_add_c_compiler_flag(-Wno-sign-compare)
3637
xcheck_add_c_compiler_flag(-Wno-missing-field-initializers)
3738
xcheck_add_c_compiler_flag(-Wno-unused-parameter)
3839
xcheck_add_c_compiler_flag(-Wno-unused-but-set-variable)
3940
xcheck_add_c_compiler_flag(-Wno-array-bounds)
40-
xcheck_add_c_compiler_flag(-Wno-format-truncation)
4141
xcheck_add_c_compiler_flag(-funsigned-char)
4242

4343
# ClangCL is command line compatible with MSVC, so 'MSVC' is set.

libbf.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3962,22 +3962,20 @@ static char *bf_ftoa_internal(size_t *plen, const bf_t *a2, int radix,
39623962
n = 1;
39633963
if ((flags & BF_FTOA_FORCE_EXP) ||
39643964
n <= -6 || n > n_max) {
3965-
const char *fmt;
39663965
/* exponential notation */
39673966
output_digits(s, a1, radix, n_digits, 1, is_dec);
39683967
if (radix_bits != 0 && radix <= 16) {
3968+
slimb_t exp_n = (n - 1) * radix_bits;
39693969
if (flags & BF_FTOA_JS_QUIRKS)
3970-
fmt = "p%+" PRId_LIMB;
3970+
dbuf_printf(s, "p%+" PRId_LIMB, exp_n);
39713971
else
3972-
fmt = "p%" PRId_LIMB;
3973-
dbuf_printf(s, fmt, (n - 1) * radix_bits);
3972+
dbuf_printf(s, "p%" PRId_LIMB, exp_n);
39743973
} else {
3974+
const char c = radix <= 10 ? 'e' : '@';
39753975
if (flags & BF_FTOA_JS_QUIRKS)
3976-
fmt = "%c%+" PRId_LIMB;
3976+
dbuf_printf(s, "%c%+" PRId_LIMB, c, n - 1);
39773977
else
3978-
fmt = "%c%" PRId_LIMB;
3979-
dbuf_printf(s, fmt,
3980-
radix <= 10 ? 'e' : '@', n - 1);
3978+
dbuf_printf(s, "%c%" PRId_LIMB, c, n - 1);
39813979
}
39823980
} else if (n <= 0) {
39833981
/* 0.x */

quickjs-libc.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ static void js_set_thread_state(JSRuntime *rt, JSThreadState *ts)
190190
js_std_cmd(/*SetOpaque*/1, rt, ts);
191191
}
192192

193+
#pragma GCC diagnostic push
194+
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
193195
static JSValue js_printf_internal(JSContext *ctx,
194196
int argc, JSValue *argv, FILE *fp)
195197
{
@@ -207,7 +209,6 @@ static JSValue js_printf_internal(JSContext *ctx,
207209
int64_t int64_arg;
208210
double double_arg;
209211
const char *string_arg;
210-
int (*dbuf_printf_fun)(DynBuf *s, const char *fmt, ...) = dbuf_printf;
211212

212213
js_std_dbuf_init(ctx, &dbuf);
213214

@@ -332,17 +333,17 @@ static JSValue js_printf_internal(JSContext *ctx,
332333
q[0] = '6';
333334
q[1] = '4';
334335
q[3] = '\0';
335-
dbuf_printf_fun(&dbuf, fmtbuf, (int64_t)int64_arg);
336+
dbuf_printf(&dbuf, fmtbuf, (int64_t)int64_arg);
336337
#else
337338
if (q >= fmtbuf + sizeof(fmtbuf) - 2)
338339
goto invalid;
339340
q[1] = q[-1];
340341
q[-1] = q[0] = 'l';
341342
q[2] = '\0';
342-
dbuf_printf_fun(&dbuf, fmtbuf, (long long)int64_arg);
343+
dbuf_printf(&dbuf, fmtbuf, (long long)int64_arg);
343344
#endif
344345
} else {
345-
dbuf_printf_fun(&dbuf, fmtbuf, (int)int64_arg);
346+
dbuf_printf(&dbuf, fmtbuf, (int)int64_arg);
346347
}
347348
break;
348349

@@ -353,7 +354,7 @@ static JSValue js_printf_internal(JSContext *ctx,
353354
string_arg = JS_ToCString(ctx, argv[i++]);
354355
if (!string_arg)
355356
goto fail;
356-
dbuf_printf_fun(&dbuf, fmtbuf, string_arg);
357+
dbuf_printf(&dbuf, fmtbuf, string_arg);
357358
JS_FreeCString(ctx, string_arg);
358359
break;
359360

@@ -369,7 +370,7 @@ static JSValue js_printf_internal(JSContext *ctx,
369370
goto missing;
370371
if (JS_ToFloat64(ctx, &double_arg, argv[i++]))
371372
goto fail;
372-
dbuf_printf_fun(&dbuf, fmtbuf, double_arg);
373+
dbuf_printf(&dbuf, fmtbuf, double_arg);
373374
break;
374375

375376
case '%':
@@ -406,6 +407,7 @@ static JSValue js_printf_internal(JSContext *ctx,
406407
dbuf_free(&dbuf);
407408
return JS_EXCEPTION;
408409
}
410+
#pragma GCC diagnostic pop // ignored "-Wformat-nonliteral"
409411

410412
uint8_t *js_load_file(JSContext *ctx, size_t *pbuf_len, const char *filename)
411413
{

quickjs.c

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,7 +3197,6 @@ JSValue JS_NewSymbol(JSContext *ctx, const char *description, bool is_global)
31973197

31983198
#define ATOM_GET_STR_BUF_SIZE 64
31993199

3200-
/* Should only be used for debug. */
32013200
static const char *JS_AtomGetStrRT(JSRuntime *rt, char *buf, int buf_size,
32023201
JSAtom atom)
32033202
{
@@ -3220,13 +3219,6 @@ static const char *JS_AtomGetStrRT(JSRuntime *rt, char *buf, int buf_size,
32203219
/* encode surrogates correctly */
32213220
utf8_encode_buf16(buf, buf_size, str->u.str16, str->len);
32223221
} else {
3223-
/* special case ASCII strings */
3224-
int i, c = 0;
3225-
for(i = 0; i < str->len; i++) {
3226-
c |= str->u.str8[i];
3227-
}
3228-
if (c < 0x80)
3229-
return (const char *)str->u.str8;
32303222
utf8_encode_buf8(buf, buf_size, str->u.str8, str->len);
32313223
}
32323224
}
@@ -6856,8 +6848,9 @@ static JSValue JS_MakeError(JSContext *ctx, JSErrorEnum error_num,
68566848
}
68576849

68586850
/* fmt and arguments may be pure ASCII or UTF-8 encoded contents */
6859-
static JSValue JS_ThrowError2(JSContext *ctx, JSErrorEnum error_num,
6860-
const char *fmt, va_list ap, bool add_backtrace)
6851+
static JSValue JS_PRINTF_FORMAT_ATTR(4, 0)
6852+
JS_ThrowError2(JSContext *ctx, JSErrorEnum error_num,
6853+
bool add_backtrace, JS_PRINTF_FORMAT const char *fmt, va_list ap)
68616854
{
68626855
char buf[256];
68636856
JSValue obj;
@@ -6871,8 +6864,9 @@ static JSValue JS_ThrowError2(JSContext *ctx, JSErrorEnum error_num,
68716864
return JS_Throw(ctx, obj);
68726865
}
68736866

6874-
static JSValue JS_ThrowError(JSContext *ctx, JSErrorEnum error_num,
6875-
const char *fmt, va_list ap)
6867+
static JSValue JS_PRINTF_FORMAT_ATTR(3, 0)
6868+
JS_ThrowError(JSContext *ctx, JSErrorEnum error_num,
6869+
JS_PRINTF_FORMAT const char *fmt, va_list ap)
68766870
{
68776871
JSRuntime *rt = ctx->rt;
68786872
JSStackFrame *sf;
@@ -6882,7 +6876,7 @@ static JSValue JS_ThrowError(JSContext *ctx, JSErrorEnum error_num,
68826876
sf = rt->current_stack_frame;
68836877
add_backtrace = !rt->in_out_of_memory &&
68846878
(!sf || (JS_GetFunctionBytecode(sf->cur_func) == NULL));
6885-
return JS_ThrowError2(ctx, error_num, fmt, ap, add_backtrace);
6879+
return JS_ThrowError2(ctx, error_num, add_backtrace, fmt, ap);
68866880
}
68876881

68886882
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,
69336927
}
69346928
}
69356929

6936-
/* never use it directly */
6937-
static JSValue JS_PRINTF_FORMAT_ATTR(3, 4) __JS_ThrowTypeErrorAtom(JSContext *ctx, JSAtom atom, JS_PRINTF_FORMAT const char *fmt, ...)
6930+
#pragma GCC diagnostic push
6931+
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
6932+
static JSValue JS_ThrowTypeErrorAtom(JSContext *ctx, const char *fmt, JSAtom atom)
69386933
{
69396934
char buf[ATOM_GET_STR_BUF_SIZE];
6940-
return JS_ThrowTypeError(ctx, fmt,
6941-
JS_AtomGetStr(ctx, buf, sizeof(buf), atom));
6935+
JS_AtomGetStr(ctx, buf, sizeof(buf), atom);
6936+
return JS_ThrowTypeError(ctx, fmt, buf);
69426937
}
69436938

6944-
/* never use it directly */
6945-
static JSValue JS_PRINTF_FORMAT_ATTR(3, 4) __JS_ThrowSyntaxErrorAtom(JSContext *ctx, JSAtom atom, JS_PRINTF_FORMAT const char *fmt, ...)
6939+
static JSValue JS_ThrowSyntaxErrorAtom(JSContext *ctx, const char *fmt, JSAtom atom)
69466940
{
69476941
char buf[ATOM_GET_STR_BUF_SIZE];
6948-
return JS_ThrowSyntaxError(ctx, fmt,
6949-
JS_AtomGetStr(ctx, buf, sizeof(buf), atom));
6942+
JS_AtomGetStr(ctx, buf, sizeof(buf), atom);
6943+
return JS_ThrowSyntaxError(ctx, fmt, buf);
69506944
}
6951-
6952-
/* %s is replaced by 'atom'. The macro is used so that gcc can check
6953-
the format string. */
6954-
#define JS_ThrowTypeErrorAtom(ctx, fmt, atom) __JS_ThrowTypeErrorAtom(ctx, atom, fmt, "")
6955-
#define JS_ThrowSyntaxErrorAtom(ctx, fmt, atom) __JS_ThrowSyntaxErrorAtom(ctx, atom, fmt, "")
6945+
#pragma GCC diagnostic pop // ignored "-Wformat-nonliteral"
69566946

69576947
static int JS_ThrowTypeErrorReadOnly(JSContext *ctx, int flags, JSAtom atom)
69586948
{
@@ -19038,7 +19028,7 @@ int JS_PRINTF_FORMAT_ATTR(2, 3) js_parse_error(JSParseState *s, JS_PRINTF_FORMAT
1903819028
int backtrace_flags;
1903919029

1904019030
va_start(ap, fmt);
19041-
JS_ThrowError2(ctx, JS_SYNTAX_ERROR, fmt, ap, false);
19031+
JS_ThrowError2(ctx, JS_SYNTAX_ERROR, false, fmt, ap);
1904219032
va_end(ap);
1904319033
backtrace_flags = 0;
1904419034
if (s->cur_func && s->cur_func->backtrace_barrier)

0 commit comments

Comments
 (0)