[Spice-devel] [spice] server: Use '%zu' to print size_t variables

Jonathon Jongsma jjongsma at redhat.com
Tue Dec 15 08:24:59 PST 2015


On Tue, 2015-12-15 at 16:54 +0100, Francois Gouget wrote:
> The size_t definition is different between 32 and 64 bit systems so that
> neither '%u' nor '%lu' work for both. '%zu' should be used instead.
> 
> Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
> ---
>  server/red-record-qxl.c | 4 ++--
>  server/red-replay-qxl.c | 8 ++++----
>  2 files changed, 6 insertions(+), 6 deletions(-)
> 
> I would have preferred to find a PRIu64-style macro but I did not find 
> any: '%zu' seems to be universally documented as the way to print size_t 
> variables although I'm not sure all compilers support this format. This 
> is probably not an issue unless this code is going to be compiled with 
> very old compilers or Visual C++ though.
> 
> An alternative would be to cast the variables to int or uint64_t. Let me 
> know if that's preferred.


I personally prefer the %zu solution.

Acked-by: Jonathon Jongsma <jjongsma at redhat.com>



> 
> diff --git a/server/red-record-qxl.c b/server/red-record-qxl.c
> index f36d0e4..9c9dd62 100644
> --- a/server/red-record-qxl.c
> +++ b/server/red-record-qxl.c
> @@ -114,7 +114,7 @@ static void write_binary(FILE *fd, const char *prefix,
> size_t size, const uint8_
>      }
>  #endif
>  
> -    fprintf(fd, "binary %d %s %ld:", WITH_ZLIB, prefix, size);
> +    fprintf(fd, "binary %d %s %zu:", WITH_ZLIB, prefix, size);
>  #if WITH_ZLIB
>      zlib_size = zlib_encode(enc, RECORD_ZLIB_DEFAULT_COMPRESSION_LEVEL, size,
>          output, sizeof(output));
> @@ -144,7 +144,7 @@ static size_t red_record_data_chunks_ptr(FILE *fd, const
> char *prefix,
>          data_size += cur->data_size;
>          count_chunks++;
>      }
> -    fprintf(fd, "data_chunks %d %ld\n", count_chunks, data_size);
> +    fprintf(fd, "data_chunks %d %zu\n", count_chunks, data_size);
>      memslot_validate_virt(slots, (intptr_t)qxl->data, memslot_id, qxl
> ->data_size, group_id);
>      write_binary(fd, prefix, qxl->data_size, qxl->data);
>  
> diff --git a/server/red-replay-qxl.c b/server/red-replay-qxl.c
> index cedd126..66acf1e 100644
> --- a/server/red-replay-qxl.c
> +++ b/server/red-replay-qxl.c
> @@ -198,7 +198,7 @@ static replay_t read_binary(SpiceReplay *replay, const
> char *prefix, size_t *siz
>      if (*buf == NULL) {
>          *buf = malloc(*size + base_size);
>          if (*buf == NULL) {
> -            spice_error("allocation error for %ld", *size);
> +            spice_error("allocation error for %zu", *size);
>              exit(1);
>          }
>      }
> @@ -259,7 +259,7 @@ static size_t red_replay_data_chunks(SpiceReplay *replay,
> const char *prefix,
>      size_t next_data_size;
>      QXLDataChunk *cur;
>  
> -    replay_fscanf(replay, "data_chunks %d %ld\n", &count_chunks, &data_size);
> +    replay_fscanf(replay, "data_chunks %d %zu\n", &count_chunks, &data_size);
>      if (base_size == 0) {
>          base_size = sizeof(QXLDataChunk);
>      }
> @@ -407,7 +407,7 @@ static QXLImage *red_replay_image(SpiceReplay *replay,
> uint32_t flags)
>          } else {
>              size = red_replay_data_chunks(replay, "bitmap.data",
> (uint8_t**)&qxl->bitmap.data, 0);
>              if (size != bitmap_size) {
> -                spice_printerr("bad image, %ld != %ld", size, bitmap_size);
> +                spice_printerr("bad image, %zu != %zu", size, bitmap_size);
>                  return NULL;
>              }
>          }
> @@ -1028,7 +1028,7 @@ static QXLSurfaceCmd *red_replay_surface_cmd(SpiceReplay
> *replay)
>          if ((qxl->flags & QXL_SURF_FLAG_KEEP_DATA) != 0) {
>              read_binary(replay, "data", &read_size, (uint8_t**)&qxl
> ->u.surface_create.data, 0);
>              if (read_size != size) {
> -                spice_printerr("mismatch %ld != %ld", size, read_size);
> +                spice_printerr("mismatch %zu != %zu", size, read_size);
>              }
>          } else {
>              qxl->u.surface_create.data = (QXLPHYSICAL)malloc(size);


More information about the Spice-devel mailing list