Skip to content

Commit f271c8e

Browse files
committed
hashmap_for_each_entry(): work around MSVC's run-time check failure #3
When compiling Git in Visual C, we do not have the luxury of support for `typeof()`, and therefore `OFFSETOF_VAR()` unfortunately has to fall back to pointer arithmetic. When compiling code using the `hashmap_for_each_entry()` macro in Debug mode, this leads to the "run-time check failure #3" because the variable passed as `var` are not initialized, yet we calculate the pointer difference `&(var->member)-var`. This "run-time check failure" causes a scary dialog to pop up. Work around this by initializing the respective variables. Note: according to the C standard, performing pointer arithmetic with `NULL` is not exactly well-defined, but it seems to work here, and it is at least better than performing pointer arithmetic with an uninitialized pointer. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 150dd57 commit f271c8e

14 files changed

+23
-23
lines changed

attr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check)
160160
* field and fill each entry with its corresponding git_attr.
161161
*/
162162
if (size != check->all_attrs_nr) {
163-
struct attr_hash_entry *e;
163+
struct attr_hash_entry *e = NULL;
164164
struct hashmap_iter iter;
165165

166166
REALLOC_ARRAY(check->all_attrs, size);

blame.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
450450
{
451451
int intersection = 0;
452452
struct hashmap_iter iter;
453-
const struct fingerprint_entry *entry_a, *entry_b;
453+
const struct fingerprint_entry *entry_a, *entry_b = NULL;
454454

455455
hashmap_for_each_entry(&b->map, &iter, entry_b,
456456
entry /* member name */) {
@@ -469,7 +469,7 @@ static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
469469
{
470470
struct hashmap_iter iter;
471471
struct fingerprint_entry *entry_a;
472-
const struct fingerprint_entry *entry_b;
472+
const struct fingerprint_entry *entry_b = NULL;
473473

474474
hashmap_iter_init(&b->map, &iter);
475475

bloom.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ struct bloom_filter *get_bloom_filter(struct repository *r,
221221

222222
if (diffopt.num_changes <= max_changes) {
223223
struct hashmap pathmap;
224-
struct pathmap_hash_entry *e;
224+
struct pathmap_hash_entry *e = NULL;
225225
struct hashmap_iter iter;
226226
hashmap_init(&pathmap, pathmap_cmp, NULL, 0);
227227

builtin/describe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
332332
if (!have_util) {
333333
struct hashmap_iter iter;
334334
struct commit *c;
335-
struct commit_name *n;
335+
struct commit_name *n = NULL;
336336

337337
init_commit_names(&commit_names);
338338
hashmap_for_each_entry(&names, &iter, n,

builtin/difftool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
344344
FILE *fp;
345345
struct hashmap working_tree_dups, submodules, symlinks2;
346346
struct hashmap_iter iter;
347-
struct pair_entry *entry;
347+
struct pair_entry *entry = NULL;
348348
struct index_state wtindex;
349349
struct checkout lstate, rstate;
350350
int rc, flags = RUN_GIT_CMD, err = 0;

builtin/fast-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static void invalidate_pack_id(unsigned int id)
498498
unsigned long lu;
499499
struct tag *t;
500500
struct hashmap_iter iter;
501-
struct object_entry *e;
501+
struct object_entry *e = NULL;
502502

503503
hashmap_for_each_entry(&object_table, &iter, e, ent) {
504504
if (e->pack_id == id)

builtin/sparse-checkout.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static int sparse_checkout_list(int argc, const char **argv)
6767

6868
if (pl.use_cone_patterns) {
6969
int i;
70-
struct pattern_entry *pe;
70+
struct pattern_entry *pe = NULL;
7171
struct hashmap_iter iter;
7272
struct string_list sl = STRING_LIST_INIT_DUP;
7373

@@ -153,7 +153,7 @@ static char *escaped_pattern(char *pattern)
153153
static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
154154
{
155155
int i;
156-
struct pattern_entry *pe;
156+
struct pattern_entry *pe = NULL;
157157
struct hashmap_iter iter;
158158
struct string_list sl = STRING_LIST_INIT_DUP;
159159
struct strbuf parent_pattern = STRBUF_INIT;
@@ -465,7 +465,7 @@ static void add_patterns_cone_mode(int argc, const char **argv,
465465
struct pattern_list *pl)
466466
{
467467
struct strbuf buffer = STRBUF_INIT;
468-
struct pattern_entry *pe;
468+
struct pattern_entry *pe = NULL;
469469
struct hashmap_iter iter;
470470
struct pattern_list existing;
471471
char *sparse_filename = get_sparse_checkout_filename();

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1953,7 +1953,7 @@ void git_configset_init(struct config_set *cs)
19531953

19541954
void git_configset_clear(struct config_set *cs)
19551955
{
1956-
struct config_set_element *entry;
1956+
struct config_set_element *entry = NULL;
19571957
struct hashmap_iter iter;
19581958
if (!cs->hash_initialized)
19591959
return;

merge-recursive.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,8 +2151,8 @@ static void handle_directory_level_conflicts(struct merge_options *opt,
21512151
struct tree *merge)
21522152
{
21532153
struct hashmap_iter iter;
2154-
struct dir_rename_entry *head_ent;
2155-
struct dir_rename_entry *merge_ent;
2154+
struct dir_rename_entry *head_ent = NULL;
2155+
struct dir_rename_entry *merge_ent = NULL;
21562156

21572157
struct string_list remove_from_head = STRING_LIST_INIT_NODUP;
21582158
struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;
@@ -2221,7 +2221,7 @@ static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)
22212221
{
22222222
struct hashmap *dir_renames;
22232223
struct hashmap_iter iter;
2224-
struct dir_rename_entry *entry;
2224+
struct dir_rename_entry *entry = NULL;
22252225
int i;
22262226

22272227
/*
@@ -2590,7 +2590,7 @@ static struct string_list *get_renames(struct merge_options *opt,
25902590
int i;
25912591
struct hashmap collisions;
25922592
struct hashmap_iter iter;
2593-
struct collision_entry *e;
2593+
struct collision_entry *e = NULL;
25942594
struct string_list *renames;
25952595

25962596
compute_collisions(&collisions, dir_renames, pairs);
@@ -2862,7 +2862,7 @@ static void initial_cleanup_rename(struct diff_queue_struct *pairs,
28622862
struct hashmap *dir_renames)
28632863
{
28642864
struct hashmap_iter iter;
2865-
struct dir_rename_entry *e;
2865+
struct dir_rename_entry *e = NULL;
28662866

28672867
hashmap_for_each_entry(dir_renames, &iter, e,
28682868
ent /* member name */) {

name-hash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ void adjust_dirname_case(struct index_state *istate, char *name)
706706

707707
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
708708
{
709-
struct cache_entry *ce;
709+
struct cache_entry *ce = NULL;
710710
unsigned int hash = memihash(name, namelen);
711711

712712
lazy_init_name_hash(istate);

revision.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static void paths_and_oids_init(struct hashmap *map)
132132
static void paths_and_oids_clear(struct hashmap *map)
133133
{
134134
struct hashmap_iter iter;
135-
struct path_and_oids_entry *entry;
135+
struct path_and_oids_entry *entry = NULL;
136136

137137
hashmap_for_each_entry(map, &iter, entry, ent /* member name */) {
138138
oidset_clear(&entry->trees);
@@ -215,7 +215,7 @@ void mark_trees_uninteresting_sparse(struct repository *r,
215215
unsigned has_interesting = 0, has_uninteresting = 0;
216216
struct hashmap map;
217217
struct hashmap_iter map_iter;
218-
struct path_and_oids_entry *entry;
218+
struct path_and_oids_entry *entry = NULL;
219219
struct object_id *oid;
220220
struct oidset_iter iter;
221221

submodule-config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void free_one_config(struct submodule_entry *entry)
8989
static void submodule_cache_clear(struct submodule_cache *cache)
9090
{
9191
struct hashmap_iter iter;
92-
struct submodule_entry *entry;
92+
struct submodule_entry *entry = NULL;
9393

9494
if (!cache->initialized)
9595
return;

t/helper/test-hashmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ int cmd__hashmap(int argc, const char **argv)
162162
while (strbuf_getline(&line, stdin) != EOF) {
163163
char *cmd, *p1 = NULL, *p2 = NULL;
164164
unsigned int hash = 0;
165-
struct test_entry *entry;
165+
struct test_entry *entry = NULL;
166166

167167
/* break line into command and up to two parameters */
168168
cmd = strtok(line.buf, DELIM);

t/helper/test-lazy-init-name-hash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ static void dump_run(void)
2929
char name[FLEX_ARRAY];
3030
};
3131

32-
struct dir_entry *dir;
33-
struct cache_entry *ce;
32+
struct dir_entry *dir = NULL;
33+
struct cache_entry *ce = NULL;
3434

3535
read_cache();
3636
if (single) {

0 commit comments

Comments
 (0)