Skip to contentSkip to conversation

Fix some "printf format" warnings.

PATCH In review Ramsay Jones 3 participants 8 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 Fix some "printf format" warnings.

---

It's interesting to note that all of the warnings are caused by mismatching parameter expression involving a call to ntohl().
[OK, it wasn't that interesting ;-)]

 builtin-ls-files.c       |    2 +-
 builtin-unpack-objects.c |    2 +-
 fetch-pack.c             |    2 +-
 index-pack.c             |    2 +-
 merge-index.c            |    2 +-
 read-cache.c             |    2 +-
 receive-pack.c           |    2 +-
 sha1_file.c              |    4 ++--
 unpack-trees.c           |    2 +-
 9 files changed, 10 insertions(+), 10 deletions(-)
builtin-ls-files.c +1 -1
index ac89eb2..5c990a5 100644
@@ -193,7 +193,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
 		putchar(line_terminator);
 	}
 	else {
-		printf("%s%06o %s %d\t",
+		printf("%s%06lo %s %d\t",
 		       tag,
 		       ntohl(ce->ce_mode),
 		       abbrev ? find_unique_abbrev(ce->sha1,abbrev)
builtin-unpack-objects.c +1 -1
index d351e02..d31d995 100644
@@ -333,7 +333,7 @@ static void unpack_all(void)
 	if (ntohl(hdr->hdr_signature) != PACK_SIGNATURE)
 		die("bad pack file");
 	if (!pack_version_ok(hdr->hdr_version))
-		die("unknown pack file version %d", ntohl(hdr->hdr_version));
+		die("unknown pack file version %ld", ntohl(hdr->hdr_version));
 	fprintf(stderr, "Unpacking %d objects\n", nr_objects);
 
 	obj_list = xmalloc(nr_objects * sizeof(*obj_list));
fetch-pack.c +1 -1
index c787106..9d86ec3 100644
@@ -510,7 +510,7 @@ static int get_pack(int xd[2])
 
 		if (read_pack_header(fd[0], &header))
 			die("protocol error: bad pack header");
-		snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u",
+		snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%lu,%lu",
 			 ntohl(header.hdr_version), ntohl(header.hdr_entries));
 		if (ntohl(header.hdr_entries) < unpack_limit)
 			do_keep = 0;
index-pack.c +1 -1
index 72e0962..01104cb 100644
@@ -167,7 +167,7 @@ static void parse_pack_header(void)
 	if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
 		die("pack signature mismatch");
 	if (!pack_version_ok(hdr->hdr_version))
-		die("pack version %d unsupported", ntohl(hdr->hdr_version));
+		die("pack version %ld unsupported", ntohl(hdr->hdr_version));
 
 	nr_objects = ntohl(hdr->hdr_entries);
 	use(sizeof(struct pack_header));
merge-index.c +1 -1
index a9983dd..2d3cac3 100644
@@ -60,7 +60,7 @@ static int merge_entry(int pos, const char *path)
 			break;
 		found++;
 		strcpy(hexbuf[stage], sha1_to_hex(ce->sha1));
-		sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT));
+		sprintf(ownbuf[stage], "%lo", ntohl(ce->ce_mode) & (~S_IFMT));
 		arguments[stage] = hexbuf[stage];
 		arguments[stage + 4] = ownbuf[stage];
 	} while (++pos < active_nr);
read-cache.c +1 -1
index c54a611..6d46dcd 100644
@@ -128,7 +128,7 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
 		changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
 		break;
 	default:
-		die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
+		die("internal error: ce_mode is %lo", ntohl(ce->ce_mode));
 	}
 	if (ce->ce_mtime.sec != htonl(st->st_mtime))
 		changed |= MTIME_CHANGED;
receive-pack.c +1 -1
index 7311c82..9e83371 100644
@@ -282,7 +282,7 @@ static const char *unpack(void)
 	hdr_err = parse_pack_header(&hdr);
 	if (hdr_err)
 		return hdr_err;
-	snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u",
+	snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%lu,%lu",
 			ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
 
 	if (ntohl(hdr.hdr_entries) < unpack_limit) {
sha1_file.c +2 -2
index 8ad7fad..80abd10 100644
@@ -592,13 +592,13 @@ static int open_packed_git_1(struct packed_git *p)
 	if (hdr.hdr_signature != htonl(PACK_SIGNATURE))
 		return error("file %s is not a GIT packfile", p->pack_name);
 	if (!pack_version_ok(hdr.hdr_version))
-		return error("packfile %s is version %u and not supported"
+		return error("packfile %s is version %lu and not supported"
 			" (try upgrading GIT to a newer version)",
 			p->pack_name, ntohl(hdr.hdr_version));
 
 	/* Verify the pack matches its index. */
 	if (num_packed_objects(p) != ntohl(hdr.hdr_entries))
-		return error("packfile %s claims to have %u objects"
+		return error("packfile %s claims to have %lu objects"
 			" while index size indicates %u objects",
 			p->pack_name, ntohl(hdr.hdr_entries),
 			num_packed_objects(p));
unpack-trees.c +1 -1
index 2e2232c..0d1d8e7 100644
@@ -539,7 +539,7 @@ static void show_stage_entry(FILE *o,
 	if (!ce)
 		fprintf(o, "%s (missing)\n", label);
 	else
-		fprintf(o, "%s%06o %s %d\t%s\n",
+		fprintf(o, "%s%06lo %s %d\t%s\n",
 			label,
 			ntohl(ce->ce_mode),
 			sha1_to_hex(ce->sha1),
Signature · 1 line
1.5.0
#1 lore raw
Quoted 11 lines · Ramsay Jones

---

It's interesting to note that all of the warnings are caused by mismatching parameter expression involving a call to ntohl().
[OK, it wasn't that interesting ;-)]

builtin-ls-files.c +0 -0
index ac89eb2..5c990a5 100644

builtin-ls-files.c static void show_ce_entry(const char *tag, struct cache_entry *ce) · line 199

Quoted 8 lines
builtin-ls-files.c +1 -1
@@ -193,7 +193,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
 		putchar(line_terminator);
 	}
 	else {
-		printf("%s%06o %s %d\t",
+		printf("%s%06lo %s %d\t",
 		       tag,
 		       ntohl(ce->ce_mode),
 		       abbrev ? find_unique_abbrev(ce->sha1,abbrev)

I think the issue is ntohl() returns uint32_t, and this did not surface as an issue so far only because that type happens to be defined as 'unsigned int' on many systems. Changing %o to %lo is shifting the breakage to other systems, isn't it?

I think we should do this instead:

	printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...
#2 lore raw

Hi Junio,

Quoted 20 lines · Junio C Hamano

builtin-ls-files.c static void show_ce_entry(const char *tag, struct cache_entry *ce) · line 199

Quoted 13 lines · Ramsay Jones
builtin-ls-files.c +1 -1
index ac89eb2..5c990a5 100644
@@ -193,7 +193,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
 		putchar(line_terminator);
 	}
 	else {
-		printf("%s%06o %s %d\t",
+		printf("%s%06lo %s %d\t",
 		       tag,
 		       ntohl(ce->ce_mode),
 		       abbrev ? find_unique_abbrev(ce->sha1,abbrev)

I think the issue is ntohl() returns uint32_t, and this did not surface as an issue so far only because that type happens to be defined as 'unsigned int' on many systems. Changing %o to %lo is shifting the breakage to other systems, isn't it?

Quoted 5 lines

I think we should do this instead:

	printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...

Oops, yes you are right.
(cygwin typedef's uint32_t as unsigned long.)

However, I would hate to add all those casts! Casts are not always evil, but should be avoided if possible. Having said that, I don't see another solution ... Hmmm, I dunno, your call ;-)

All the best,

Ramsay Jones

#3 lore raw
Quoted 6 lines · Ramsay Jones
Quoted 6 lines

builtin-ls-files.c static void show_ce_entry(const char *tag, struct cache_entry *ce) · line 198

Quoted 4 lines
builtin-ls-files.c +1 -1
-        printf("%s%06o %s %d\t",
+        printf("%s%06lo %s %d\t",
                tag,
                ntohl(ce->ce_mode), 

I think we should do this instead:

Quoted 7 lines
Quoted 1 line
    printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...

Oops, yes you are right.
(cygwin typedef's uint32_t as unsigned long.)

However, I would hate to add all those casts! Casts are not always evil, but should be avoided if possible. Having said that, I don't see another solution ...

shouldn't it be something like this?

printf("%s%06"PRIo32" %s %d\t", tag, ntohl(ce->ce_mode), ...)

that's the correct and allegedly portable way I guess.

cheers
simon

Signature · 4 lines
Serve - BSD     +++  RENT this banner advert  +++    ASCII Ribbon   /"\
Work - Mac      +++  space for low €€€ NOW!1  +++      Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \
#4 lore raw
Quoted 15 lines · Simon 'corecode' Schubert
Quoted 6 lines · Ramsay Jones
Quoted 6 lines

builtin-ls-files.c static void show_ce_entry(const char *tag, struct cache_entry *ce) · line 198

Quoted 4 lines
builtin-ls-files.c +1 -1
-        printf("%s%06o %s %d\t",
+        printf("%s%06lo %s %d\t",
                tag,
                ntohl(ce->ce_mode), 

I think we should do this instead:

Quoted 7 lines
Quoted 1 line
    printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...

Oops, yes you are right.
(cygwin typedef's uint32_t as unsigned long.)

However, I would hate to add all those casts! Casts are not always evil, but should be avoided if possible. Having said that, I don't see another solution ...

Quoted 9 lines

shouldn't it be something like this?

printf("%s%06"PRIo32" %s %d\t", tag, ntohl(ce->ce_mode), ...)

that's the correct and allegedly portable way I guess.

cheers
simon

Yes, that would work, but again I was trying not to depend on a C99 header file (namely <inttypes.h>).

I suppose I should just assume that git now requires these C99 headers!

Junio, what do you think about Simon's solution?

ATB,

Ramsay Jones

#5 lore raw
In reply to Simon 'corecode' Schubert
Quoted 16 lines · Simon 'corecode' Schubert
Quoted 6 lines · Ramsay Jones
Quoted 6 lines

builtin-ls-files.c static void show_ce_entry(const char *tag, struct cache_entry *ce) · line 198

Quoted 4 lines
builtin-ls-files.c +1 -1
-        printf("%s%06o %s %d\t",
+        printf("%s%06lo %s %d\t",
                tag,
                ntohl(ce->ce_mode),

I think we should do this instead:

Quoted 8 lines
Quoted 1 line
    printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...

Oops, yes you are right.
(cygwin typedef's uint32_t as unsigned long.)

However, I would hate to add all those casts! Casts are not always evil, but should be avoided if possible. Having said that, I don't see another solution ...

Quoted 5 lines

shouldn't it be something like this?

printf("%s%06"PRIo32" %s %d\t", tag, ntohl(ce->ce_mode), ...)

that's the correct and allegedly portable way I guess.

Yes, except that that is only portable across platforms with inttypes.h, and we would need a compatibility definition in git-compat-util.h next to PRIuMAX definition we already have.

But I wonder if this is really worth it. The thing is,

    printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...

is perfectly readable for even old timers about git, as long as they know traditional C and what ntohl() is. And ce->ce_mode even fits in 16-bit, so while we are _not_ supporting platforms whose unsigned int is 16-bit, the above cast is not losing any useful precision either.

#6 lore raw
Quoted 18 lines · Junio C Hamano
Quoted 16 lines · Simon 'corecode' Schubert
Quoted 6 lines · Ramsay Jones
Quoted 6 lines

builtin-ls-files.c static void show_ce_entry(const char *tag, struct cache_entry *ce) · line 198

Quoted 4 lines
builtin-ls-files.c +1 -1
-        printf("%s%06o %s %d\t",
+        printf("%s%06lo %s %d\t",
                tag,
                ntohl(ce->ce_mode),

I think we should do this instead:

Quoted 7 lines
Quoted 1 line
    printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...

Oops, yes you are right.
(cygwin typedef's uint32_t as unsigned long.)

However, I would hate to add all those casts! Casts are not always evil, but should be avoided if possible. Having said that, I don't see another solution ...

shouldn't it be something like this?

Quoted 8 lines
Quoted 3 lines

printf("%s%06"PRIo32" %s %d\t", tag, ntohl(ce->ce_mode), ...)

that's the correct and allegedly portable way I guess.

Yes, except that that is only portable across platforms with inttypes.h, and we would need a compatibility definition in git-compat-util.h next to PRIuMAX definition we already have.

Hmmm, I only have the 1.5.0 code, and so I don't know what the current code looks like; so if this is rubbish, please ignore...

In version 1.4.4, the only files to depend on <stdint.h> were {arm,ppc}/sha1.[ch]. If this was a problem on any given arm/ppc platform, you could configure the software to use the OpenSSL or Mozilla versions instead.

In 1.5.0, however, git-compat-util.h _unconditionally_ includes <inttypes.h>, which in turn includes <stdint.h>. Now the files which depend on definitions in <stdint.h> includes:
{arm,ppc}/sha1.[ch]
cache.h
pack.h
builtin-pack-objects.c
fast-import.c
sha1_file.c
(the symbols being: uint32_t, uint64_t, uint16_t, uintmax_t) Version 1.5.0 does not seem to depend on any symbols in the <inttypes.h> header file...

So, at least for 1.5.0, those header files are already required.

I don't know if that is desired ;-)

ATB,

Ramsay Jones

#7 lore raw
Quoted 3 lines · Ramsay Jones
Quoted 3 lines
Quoted 3 lines

printf("%s%06"PRIo32" %s %d\t", tag, ntohl(ce->ce_mode), ...)

that's the correct and allegedly portable way I guess.

I do not think this is really worth it.

    printf("%s%06o %s %d\t", tag, (unsigned) ntohl(ce->ce_mode), ...

is perfectly readable for even old timers about git, as long as they know traditional C and what ntohl() is. And ce->ce_mode even fits in 16-bit, so while we are _not_ supporting platforms whose unsigned int is 16-bit, the above cast is not losing any useful precision either.

Quoted 1 line

(the symbols being: uint32_t, uint64_t, uint16_t, uintmax_t)

I think we would want these exact-sized integer types, so if they are missing the platform ports may want to supply their own substitute.