From 595afc6f4ba701f776c9fa86826464d9ab6226f1 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Tue, 30 Sep 2025 09:31:40 -0700 Subject: [PATCH] portability: add missing PRI* constants, signed 1 in bitfield hack Add missing uses of PRI constants. Create a PRI constant for size_t, since %z isn't available on all platforms. Notably, the legacy Windows runtime needs %I instead of %z. Use that on UCRT as well, since there doesn't seem to be a way to determine if you are compiling for MSVCRT or UCRT. Signed-off-by: H. Peter Anvin (Intel) --- asm/exprdump.c | 7 ++++--- asm/preproc.c | 5 +++-- include/compiler.h | 24 ++++++++++++++++++++++++ include/nasmlib.h | 2 +- output/outaout.c | 2 +- output/outas86.c | 2 +- output/outbin.c | 2 +- output/outmacho.c | 5 +++-- 8 files changed, 38 insertions(+), 11 deletions(-) diff --git a/asm/exprdump.c b/asm/exprdump.c index 68a9b670d..cf0f29dcc 100644 --- a/asm/exprdump.c +++ b/asm/exprdump.c @@ -61,9 +61,9 @@ static const char *expr_type(int32_t type) if (type >= EXPR_REG_START && type <= EXPR_REG_END) { return nasm_reg_names[type - EXPR_REG_START]; } else if (type >= EXPR_SEGBASE) { - snprintf(seg_str, sizeof seg_str, "%sseg %d", + snprintf(seg_str, sizeof seg_str, "%sseg %lu", (type - EXPR_SEGBASE) == location.segment ? "this " : "", - type - EXPR_SEGBASE); + (unsigned long)(type - EXPR_SEGBASE)); return seg_str; } else { return "ERR"; @@ -74,6 +74,7 @@ void dump_expr(const expr *e) { printf("["); for (; e->type; e++) - printf("<%s(%d),%"PRId64">", expr_type(e->type), e->type, e->value); + printf("<%s(%"PRId32"),%"PRId64">", + expr_type(e->type), e->type, e->value); printf("]\n"); } diff --git a/asm/preproc.c b/asm/preproc.c index 63b30bc30..99d05d012 100644 --- a/asm/preproc.c +++ b/asm/preproc.c @@ -5105,7 +5105,7 @@ static int do_directive(Token *tline, Token **output, bool suppressed) pop_mstk(&defining->dstk, defining->dstk.mstk); if (defining->refcnt != 1) - nasm_panic("defining->refcnt == %zu", defining->refcnt); + nasm_panic("defining->refcnt == %"PRIzu, defining->refcnt); mmhead = (MMacro **) hash_findi_add(&mmacros, defining->name); defining->next = *mmhead; @@ -6803,7 +6803,8 @@ static MMacro *use_mmacro(MMacro *m, int *nparamp, Token ***paramsp) if (m->in_progress > m->max_depth) { if (m->max_depth > 0) { /* Document this properly when recursive mmacros re-implemented */ - nasm_warn(WARN_OTHER, "reached maximum recursion depth of %i", + nasm_warn(WARN_OTHER, + "reached maximum recursion depth of %"PRId32, m->max_depth); } nasm_free(params); diff --git a/include/compiler.h b/include/compiler.h index 17d82f08c..71a37487d 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -99,6 +99,30 @@ # include #endif +/* + * This is impossible to do 100% accurately, because the actual type + * of size_t may differ from its range. + */ +#ifdef _WIN32 +# define PRIz "I" /* Needed for msvcrt, not ucrt */ +#elif defined(PRINTF_SUPPORTS_Z) || \ + (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) +# define PRIz "z" +#elif SIZE_MAX == UINT_MAX +# define PRIz "" +#elif SIZE_MAX == ULONG_MAX +# define PRIz "l" +#elif SIZE_MAX == ULONGLONG_MAX +# define PRIz "ll" +#else +# error "Unable to determine printf format for size_t" +#endif +#define PRIzd PRIz "u" /* size_t is always unsigned */ +#define PRIzu PRIz "u" +#define PRIzu PRIz "u" +#define PRIzx PRIz "x" +#define PRIzX PRIz "X" + #ifdef HAVE_STDBIT_H # include diff --git a/include/nasmlib.h b/include/nasmlib.h index a3f2227f7..84adf9572 100644 --- a/include/nasmlib.h +++ b/include/nasmlib.h @@ -75,7 +75,7 @@ typedef union intorptr intorptr; #define mk_field(name,pos,width) \ mk_field_mask(name,pos,width, \ - (ONE << name ## _WIDTH)-1) + (1 << name ## _WIDTH)-1) /* * Cast a value to a suitable type to represent the encoded diff --git a/output/outaout.c b/output/outaout.c index f1e676044..2fda23e49 100644 --- a/output/outaout.c +++ b/output/outaout.c @@ -585,7 +585,7 @@ static void aout_out(const struct out_data *out) s = NULL; else { nasm_warn(WARN_OTHER, "attempt to assemble code in" - " segment %d: defaulting to `.text'", segto); + " unknown section: defaulting to `.text'"); s = &stext; } diff --git a/output/outas86.c b/output/outas86.c index c3911354c..04b2ef497 100644 --- a/output/outas86.c +++ b/output/outas86.c @@ -298,7 +298,7 @@ static void as86_out(const struct out_data *out) s = NULL; else { nasm_warn(WARN_OTHER, "attempt to assemble code in" - " segment %d: defaulting to `.text'", segto); + " unknown section: defaulting to `.text'"); s = &stext; } diff --git a/output/outbin.c b/output/outbin.c index 22c3845d0..587abccdb 100644 --- a/output/outbin.c +++ b/output/outbin.c @@ -1517,7 +1517,7 @@ static void write_srecord(unsigned int len, unsigned int alen, csum += dptr[i]; csum = 0xff-csum; - p += sprintf(p, "S%c%02X%0*X", type, len+alen+1, alen*2, addr); + p += sprintf(p, "S%c%02X%0*"PRIX32, type, len+alen+1, alen*2, addr); for (i = 0; i < len; i++) p += sprintf(p, "%02X", dptr[i]); p += sprintf(p, "%02X\n", csum); diff --git a/output/outmacho.c b/output/outmacho.c index c80b4526d..8ef787dba 100644 --- a/output/outmacho.c +++ b/output/outmacho.c @@ -556,7 +556,7 @@ static void macho_output(const struct out_data *out) s = get_section_by_index(secto); if (!s) { nasm_warn(WARN_OTHER, "attempt to assemble code in" - " section %d: defaulting to `.text'", secto); + " unknown section: defaulting to `.text'"); s = get_section_by_name("__TEXT", "__text"); /* should never happen */ @@ -1082,7 +1082,8 @@ static void macho_symdef(char *name, int32_t section, int64_t offset, /* give an error on unfound section if it's not an ** external or common symbol (assemble_file() does a ** seg_alloc() on every call for them) */ - nasm_panic("in-file index for section %d not found, is_global = %d", section, is_global); + nasm_panic("in-file index for section %"PRId32" not found, " + "is_global = %d", section, is_global); break; } }