From 82a7e8c4ac25a13011e1b23f04faba7a52258e2a Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 26 Jan 2020 10:59:07 +0000 Subject: [PATCH] New wrapper macro for printf("%zu"), for old VS compat. A user reports that Visual Studio 2013 and earlier have printf implementations in their C library that don't support the 'z' modifier to indicate that an integer argument is size_t. The 'I' modifier apparently works in place of it. To avoid littering ifdefs everywhere, I've invented my own inttypes.h style macros to wrap size_t formatting directives, which are defined to %zu and %zx normally, or %Iu and %Ix in old-VS mode. Those are in defs.h, and they're used everywhere that a %z might otherwise get into the Windows build. --- defs.h | 7 +++++++ logging.c | 7 ++++--- pageant.c | 11 ++++++----- sshprng.c | 8 ++++---- sshpubk.c | 4 ++-- sshrsa.c | 4 ++-- testcrypt.c | 2 +- testsc.c | 4 ++-- windows/winpgnt.c | 2 +- 9 files changed, 29 insertions(+), 20 deletions(-) diff --git a/defs.h b/defs.h index 0a4f08c8..b9577036 100644 --- a/defs.h +++ b/defs.h @@ -22,9 +22,16 @@ #define PRIdMAX "I64d" #define PRIXMAX "I64X" #define SCNu64 "I64u" +#define SIZEx "Ix" +#define SIZEu "Iu" uintmax_t strtoumax(const char *nptr, char **endptr, int base); #else #include +/* Because we still support older MSVC libraries which don't recognise the + * standard C "z" modifier for size_t-sized integers, we must use an + * inttypes.h-style macro for those */ +#define SIZEx "zx" +#define SIZEu "zu" #endif #if defined __GNUC__ || defined __clang__ diff --git a/logging.c b/logging.c index d61038c9..a12caa5a 100644 --- a/logging.c +++ b/logging.c @@ -344,7 +344,7 @@ void log_packet(LogContext *ctx, int direction, int type, /* If we're about to stop omitting, it's time to say how * much we omitted. */ if ((blktype != PKTLOG_OMIT) && omitted) { - logprintf(ctx, " (%zu byte%s omitted)\r\n", + logprintf(ctx, " (%"SIZEu" byte%s omitted)\r\n", omitted, (omitted==1?"":"s")); omitted = 0; } @@ -352,7 +352,8 @@ void log_packet(LogContext *ctx, int direction, int type, /* (Re-)initialise dumpdata as necessary * (start of row, or if we've just stopped omitting) */ if (!output_pos && !omitted) - sprintf(dumpdata, " %08zx%*s\r\n", p-(p%16), 1+3*16+2+16, ""); + sprintf(dumpdata, " %08"SIZEx"%*s\r\n", + p-(p%16), 1+3*16+2+16, ""); /* Deal with the current byte. */ if (blktype == PKTLOG_OMIT) { @@ -387,7 +388,7 @@ void log_packet(LogContext *ctx, int direction, int type, /* Tidy up */ if (omitted) - logprintf(ctx, " (%zu byte%s omitted)\r\n", + logprintf(ctx, " (%"SIZEu" byte%s omitted)\r\n", omitted, (omitted==1?"":"s")); logflush(ctx); } diff --git a/pageant.c b/pageant.c index b43ef33e..f76f7f62 100644 --- a/pageant.c +++ b/pageant.c @@ -978,10 +978,10 @@ static void pageant_conn_closing(Plug *plug, const char *error_msg, struct pageant_conn_state *pc = container_of( plug, struct pageant_conn_state, plug); if (error_msg) - pageant_listener_client_log(pc->plc, "c#%zu: error: %s", + pageant_listener_client_log(pc->plc, "c#%"SIZEu": error: %s", pc->conn_index, error_msg); else - pageant_listener_client_log(pc->plc, "c#%zu: connection closed", + pageant_listener_client_log(pc->plc, "c#%"SIZEu": connection closed", pc->conn_index); sk_close(pc->connsock); pageant_unregister_client(&pc->pc); @@ -1010,7 +1010,7 @@ static void pageant_conn_log(PageantClient *pc, PageantClientRequestId *reqid, container_of(reqid, struct pageant_conn_queued_response, reqid); char *formatted = dupvprintf(fmt, ap); - pageant_listener_client_log(pcs->plc, "c#%zu,r#%zu: %s", + pageant_listener_client_log(pcs->plc, "c#%"SIZEu",r#%"SIZEu": %s", pcs->conn_index, qr->req_index, formatted); sfree(formatted); } @@ -1159,10 +1159,11 @@ static int pageant_listen_accepting(Plug *plug, peerinfo = sk_peer_info(pc->connsock); if (peerinfo && peerinfo->log_text) { - pageant_listener_client_log(pl->plc, "c#%zu: new connection from %s", + pageant_listener_client_log(pl->plc, + "c#%"SIZEu": new connection from %s", pc->conn_index, peerinfo->log_text); } else { - pageant_listener_client_log(pl->plc, "c#%zu: new connection", + pageant_listener_client_log(pl->plc, "c#%"SIZEu": new connection", pc->conn_index); } sk_free_peer_info(peerinfo); diff --git a/sshprng.c b/sshprng.c index ea09ab04..59307e9d 100644 --- a/sshprng.c +++ b/sshprng.c @@ -177,7 +177,7 @@ static void prng_seed_BinarySink_write( prng *pr = BinarySink_DOWNCAST(bs, prng); prng_impl *pi = container_of(pr, prng_impl, Prng); assert(pi->keymaker); - prngdebug("prng: got %zu bytes of seed\n", len); + prngdebug("prng: got %"SIZEu" bytes of seed\n", len); put_data(pi->keymaker, data, len); } @@ -228,7 +228,7 @@ void prng_read(prng *pr, void *vout, size_t size) assert(!pi->keymaker); - prngdebug("prng_read %zu\n", size); + prngdebug("prng_read %"SIZEu"\n", size); uint8_t *out = (uint8_t *)vout; for (; size > 0; size--) { @@ -256,7 +256,7 @@ void prng_add_entropy(prng *pr, unsigned source_id, ptrlen data) index++; } - prngdebug("prng_add_entropy source=%u size=%zu -> collector %zi\n", + prngdebug("prng_add_entropy source=%u size=%"SIZEu" -> collector %zi\n", source_id, data.len, index); put_datapl(pi->collectors[index], data); @@ -272,7 +272,7 @@ void prng_add_entropy(prng *pr, unsigned source_id, ptrlen data) uint32_t reseed_index = ++pi->reseeds; prngdebug("prng entropy reseed #%"PRIu32"\n", reseed_index); for (size_t i = 0; i < NCOLLECTORS; i++) { - prngdebug("emptying collector %zu\n", i); + prngdebug("emptying collector %"SIZEu"\n", i); ssh_hash_digest(pi->collectors[i], pi->pending_output); put_data(&pi->Prng, pi->pending_output, pi->hashalg->hlen); ssh_hash_reset(pi->collectors[i]); diff --git a/sshpubk.c b/sshpubk.c index 5b1ff051..614324d1 100644 --- a/sshpubk.c +++ b/sshpubk.c @@ -1421,8 +1421,8 @@ char *ssh1_pubkey_str(RSAKey *key) dec1 = mp_get_decimal(key->exponent); dec2 = mp_get_decimal(key->modulus); - buffer = dupprintf("%zd %s %s%s%s", mp_get_nbits(key->modulus), dec1, dec2, - key->comment ? " " : "", + buffer = dupprintf("%"SIZEu" %s %s%s%s", mp_get_nbits(key->modulus), + dec1, dec2, key->comment ? " " : "", key->comment ? key->comment : ""); sfree(dec1); sfree(dec2); diff --git a/sshrsa.c b/sshrsa.c index 0004f86f..2ca72342 100644 --- a/sshrsa.c +++ b/sshrsa.c @@ -296,7 +296,7 @@ char *rsa_ssh1_fingerprint(RSAKey *key) ssh_hash_final(hash, digest); out = strbuf_new(); - strbuf_catf(out, "%zu ", mp_get_nbits(key->modulus)); + strbuf_catf(out, "%"SIZEu" ", mp_get_nbits(key->modulus)); for (i = 0; i < 16; i++) strbuf_catf(out, "%s%02x", i ? ":" : "", digest[i]); if (key->comment) @@ -788,7 +788,7 @@ char *rsa2_invalid(ssh_key *key, unsigned flags) const ssh_hashalg *halg = rsa2_hash_alg_for_flags(flags, &sign_alg_name); if (nbytes < rsa_pkcs1_length_of_fixed_parts(halg)) { return dupprintf( - "%zu-bit RSA key is too short to generate %s signatures", + "%"SIZEu"-bit RSA key is too short to generate %s signatures", bits, sign_alg_name); } diff --git a/testcrypt.c b/testcrypt.c index 6b9238b3..30ee49ea 100644 --- a/testcrypt.c +++ b/testcrypt.c @@ -1286,7 +1286,7 @@ int main(int argc, char **argv) for (size_t i = 0; i < sb->len; i++) if (sb->s[i] == '\n') lines++; - fprintf(outfp, "%zu\n%s", lines, sb->s); + fprintf(outfp, "%"SIZEu"\n%s", lines, sb->s); fflush(outfp); strbuf_free(sb); sfree(line); diff --git a/testsc.c b/testsc.c index 0824c5e2..50bda39f 100644 --- a/testsc.c +++ b/testsc.c @@ -161,7 +161,7 @@ VOLATILE_WRAPPED_DEFN(, void, log_to_file, (const char *filename)) static const char *outdir = NULL; char *log_filename(const char *basename, size_t index) { - return dupprintf("%s/%s.%04zu", outdir, basename, index); + return dupprintf("%s/%s.%04"SIZEu, outdir, basename, index); } static char *last_filename; @@ -1572,7 +1572,7 @@ int main(int argc, char **argv) printf("All tests passed\n"); return 0; } else { - printf("%zu tests failed\n", nrun - npass); + printf("%"SIZEu" tests failed\n", nrun - npass); return 1; } } diff --git a/windows/winpgnt.c b/windows/winpgnt.c index da9e5583..a4c69c02 100644 --- a/windows/winpgnt.c +++ b/windows/winpgnt.c @@ -909,7 +909,7 @@ static char *answer_filemapping_message(const char *mapname) mapsize = mbi.RegionSize; } #ifdef DEBUG_IPC - debug("region size = %zd\n", mapsize); + debug("region size = %"SIZEu"\n", mapsize); #endif if (mapsize < 5) { err = dupstr("mapping smaller than smallest possible request");