Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38dc994

Browse files
mploughcarlosmn
authored andcommitted
Fix libgit2#3094 - improve use of portable size_t/ssize_t format specifiers.
The header src/cc-compat.h defines portable format specifiers PRIuZ, PRIdZ, and PRIxZ. The original report highlighted the need to use these specifiers in examples/network/fetch.c. For this commit, I checked all C source and header files not in deps/ and transitioned to the appropriate format specifier where appropriate.
1 parent 34ff34f commit 38dc994

File tree

11 files changed

+20
-20
lines changed

11 files changed

+20
-20
lines changed

examples/network/fetch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ int fetch(git_repository *repo, int argc, char **argv)
143143
* network.
144144
*/
145145
if (stats->local_objects > 0) {
146-
printf("\rReceived %d/%d objects in %zu bytes (used %d local objects)\n",
146+
printf("\rReceived %d/%d objects in %" PRIuZ " bytes (used %d local objects)\n",
147147
stats->indexed_objects, stats->total_objects, stats->received_bytes, stats->local_objects);
148148
} else{
149-
printf("\rReceived %d/%d objects in %zu bytes\n",
149+
printf("\rReceived %d/%d objects in %" PRIuZ "bytes\n",
150150
stats->indexed_objects, stats->total_objects, stats->received_bytes);
151151
}
152152

src/cache.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ void git_cache_dump_stats(git_cache *cache)
5050
if (kh_size(cache->map) == 0)
5151
return;
5252

53-
printf("Cache %p: %d items cached, %d bytes\n",
54-
cache, kh_size(cache->map), (int)cache->used_memory);
53+
printf("Cache %p: %d items cached, %"PRIdZ" bytes\n",
54+
cache, kh_size(cache->map), cache->used_memory);
5555

5656
kh_foreach_value(cache->map, object, {
5757
char oid_str[9];
58-
printf(" %s%c %s (%d)\n",
58+
printf(" %s%c %s (%"PRIuZ")\n",
5959
git_object_type2string(object->type),
6060
object->flags == GIT_CACHE_STORE_PARSED ? '*' : ' ',
6161
git_oid_tostr(oid_str, sizeof(oid_str), &object->oid),
62-
(int)object->size
62+
object->size
6363
);
6464
});
6565
}

src/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,8 +1299,8 @@ static int checkout_get_actions(
12991299
if (counts[CHECKOUT_ACTION__CONFLICT] > 0 &&
13001300
(data->strategy & GIT_CHECKOUT_ALLOW_CONFLICTS) == 0)
13011301
{
1302-
giterr_set(GITERR_CHECKOUT, "%d %s checkout",
1303-
(int)counts[CHECKOUT_ACTION__CONFLICT],
1302+
giterr_set(GITERR_CHECKOUT, "%"PRIuZ" %s checkout",
1303+
counts[CHECKOUT_ACTION__CONFLICT],
13041304
counts[CHECKOUT_ACTION__CONFLICT] == 1 ?
13051305
"conflict prevents" : "conflicts prevent");
13061306
error = GIT_ECONFLICT;

src/merge.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ int merge_bases_many(git_commit_list **out, git_revwalk **walk_out, git_reposito
7979
unsigned int i;
8080

8181
if (length < 2) {
82-
giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %u.", length);
82+
giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %" PRIuZ ".", length);
8383
return -1;
8484
}
8585

@@ -185,7 +185,7 @@ int git_merge_base_octopus(git_oid *out, git_repository *repo, size_t length, co
185185
assert(out && repo && input_array);
186186

187187
if (length < 2) {
188-
giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %u.", length);
188+
giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %" PRIuZ ".", length);
189189
return -1;
190190
}
191191

@@ -2451,7 +2451,7 @@ int git_merge__check_result(git_repository *repo, git_index *index_new)
24512451
goto done;
24522452

24532453
if ((conflicts = index_conflicts + wd_conflicts) > 0) {
2454-
giterr_set(GITERR_MERGE, "%d uncommitted change%s would be overwritten by merge",
2454+
giterr_set(GITERR_MERGE, "%" PRIuZ " uncommitted change%s would be overwritten by merge",
24552455
conflicts, (conflicts != 1) ? "s" : "");
24562456
error = GIT_ECONFLICT;
24572457
}

src/rebase.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ static int rebase_setupfiles_merge(git_rebase *rebase)
436436
size_t i;
437437
int error = 0;
438438

439-
if ((error = rebase_setupfile(rebase, END_FILE, -1, "%d\n", git_array_size(rebase->operations))) < 0 ||
439+
if ((error = rebase_setupfile(rebase, END_FILE, -1, "%" PRIuZ "\n", git_array_size(rebase->operations))) < 0 ||
440440
(error = rebase_setupfile(rebase, ONTO_NAME_FILE, -1, "%s\n", rebase->onto_name)) < 0)
441441
goto done;
442442

@@ -789,7 +789,7 @@ static int rebase_next_merge(
789789
normalize_checkout_options_for_apply(&checkout_opts, rebase, current_commit);
790790

791791
if ((error = git_indexwriter_init_for_operation(&indexwriter, rebase->repo, &checkout_opts.checkout_strategy)) < 0 ||
792-
(error = rebase_setupfile(rebase, MSGNUM_FILE, -1, "%d\n", rebase->current+1)) < 0 ||
792+
(error = rebase_setupfile(rebase, MSGNUM_FILE, -1, "%" PRIuZ "\n", rebase->current+1)) < 0 ||
793793
(error = rebase_setupfile(rebase, CURRENT_FILE, -1, "%.*s\n", GIT_OID_HEXSZ, current_idstr)) < 0 ||
794794
(error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, NULL)) < 0 ||
795795
(error = git_merge__check_result(rebase->repo, index)) < 0 ||

src/stash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ static int ensure_clean_index(git_repository *repo, git_index *index)
770770
goto done;
771771

772772
if (git_diff_num_deltas(index_diff) > 0) {
773-
giterr_set(GITERR_STASH, "%d uncommitted changes exist in the index",
773+
giterr_set(GITERR_STASH, "%" PRIuZ " uncommitted changes exist in the index",
774774
git_diff_num_deltas(index_diff));
775775
error = GIT_EUNCOMMITTED;
776776
}

src/transports/http.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ static int write_chunk(git_stream *io, const char *buffer, size_t len)
511511
git_buf buf = GIT_BUF_INIT;
512512

513513
/* Chunk header */
514-
git_buf_printf(&buf, "%X\r\n", (unsigned)len);
514+
git_buf_printf(&buf, "%" PRIxZ "\r\n", len);
515515

516516
if (git_buf_oom(&buf))
517517
return -1;

src/transports/smart_pkt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ static int buffer_want_with_caps(const git_remote_head *head, transport_smart_ca
523523

524524
if (len > 0xffff) {
525525
giterr_set(GITERR_NET,
526-
"Tried to produce packet with invalid length %d", len);
526+
"Tried to produce packet with invalid length %" PRIuZ, len);
527527
return -1;
528528
}
529529

tests/blame/blame_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ void hunk_message(size_t idx, const git_blame_hunk *hunk, const char *fmt, ...)
44
{
55
va_list arglist;
66

7-
printf("Hunk %zd (line %d +%d): ", idx,
7+
printf("Hunk %"PRIuZ" (line %d +%d): ", idx,
88
hunk->final_start_line_number, hunk->lines_in_hunk-1);
99

1010
va_start(arglist, fmt);

tests/clar_libgit2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,8 @@ void clar__assert_equal_file(
483483
for (pos = 0; pos < bytes && expected_data[pos] == buf[pos]; ++pos)
484484
/* find differing byte offset */;
485485
p_snprintf(
486-
buf, sizeof(buf), "file content mismatch at byte %d",
487-
(int)(total_bytes + pos));
486+
buf, sizeof(buf), "file content mismatch at byte %"PRIdZ,
487+
(ssize_t)(total_bytes + pos));
488488
p_close(fd);
489489
clar__fail(file, line, path, buf, 1);
490490
}

tests/merge/merge_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void merge__dump_index_entries(git_vector *index_entries)
110110
size_t i;
111111
const git_index_entry *index_entry;
112112

113-
printf ("\nINDEX [%d]:\n", (int)index_entries->length);
113+
printf ("\nINDEX [%"PRIuZ"]:\n", index_entries->length);
114114
for (i = 0; i < index_entries->length; i++) {
115115
index_entry = index_entries->contents[i];
116116

0 commit comments

Comments
 (0)