git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 10/10] hashmap_for_each_entry(): work around MSVC's run-time check failure #3

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 26, 2020, 16:54 UTC
Message-ID
<xmqqy2kwiimi.fsf@gitster.c.googlers.com>
In-Reply-To
<xmqq8scxln10.fsf@gitster.c.googlers.com>
Junio C Hamano <gitster@pobox.com> writes:
> Whoa, wait.  If it is just that macro, can we perhaps do something
> like the attached patch?

I looked at all the uses of OFFSETOF_VAR() and I think the one used for hashmap_for_each_entry() is the only instance that 'var' given to it can legitimately be uninitialized, if typeof() were available.

Here are the findings.
#define hashmap_put_entry(map, keyvar, member) \
	container_of_or_null_offset(hashmap_put(map, &(keyvar)->member), \
				OFFSETOF_VAR(keyvar, member))

The keyvar is a pointer to the entry being placed in the map; it must hold a valid one so the pointer-diff implementation of OFFSETOF_VAR() should work fine, or we are putting garbage in to the map.

#define hashmap_remove_entry(map, keyvar, member, keydata) \
	container_of_or_null_offset( \
			hashmap_remove(map, &(keyvar)->member, keydata), \
			OFFSETOF_VAR(keyvar, member))

The keyvar is used to match against an existing entry in the map to be removed---it must have a valid value.

#define hashmap_for_each_entry(map, iter, var, member) \
	for (var = hashmap_iter_first_entry_offset(map, iter, \
						OFFSETOF_VAR(var, member)); \
		var; \
		var = hashmap_iter_next_entry_offset(iter, \
						OFFSETOF_VAR(var, member)))

This, as you discovered, can be fed an uninitialized var and the first thing it does is to use OFFSETOF_VAR() on it in order to call hashmap_iter_first_entry_offset(). After that, i.e. when we called that function to start the loop, var is defined and we would be OK.

The trick I suggested is to initialize var to NULL before making the call to hashmap_iter_first_entry_offset(), i.e.

	for (var = NULL, \
	     var = hashmap_iter_first_entry_offset(map, iter, \
						OFFSETOF_VAR(var, member)); \
#define hashmap_get_entry(map, keyvar, member, keydata) \
	container_of_or_null_offset( \
				hashmap_get(map, &(keyvar)->member, keydata), \
				OFFSETOF_VAR(keyvar, member))
Must be OK for the same reason _put_entry() is OK.
#define hashmap_get_next_entry(map, var, member) \
	container_of_or_null_offset(hashmap_get_next(map, &(var)->member), \
				OFFSETOF_VAR(var, member))

This tries to go to the next-equal-pointer starting from var, so var must be valid already.

So, perhaps the attached may be a viable replacement that would be more futureproof with less maintenance cost, I suspect.

Thanks.
--- >8 ----- cut here ----- >8 ---
Subject: hashmap_for_each_entry(): workaround MSVC's runtime check failure #3

The OFFSETOF_VAR(var, member) macro is implemented in terms of offsetof(typeof(*var), member) with compilers that know typeof(), but its fallback implemenation compares &(var->member) and (var) and count the distance in bytes, i.e.

    ((uintptr_t)&(var)->member - (uintptr_t)(var))

MSVC's runtime check, when fed an uninitialized 'var', flags this as a use of an uninitialized variable (and that is legit---uninitialized contents of 'var' is subtracted) in a debug build.

After auditing all 6 uses of OFFSETOF_VAR(), 1 of them does feed a potentially uninitialized 'var' to the macro in the beginning of the for() loop:

    #define hashmap_for_each_entry(map, iter, var, member) \
            for (var = hashmap_iter_first_entry_offset(map, iter, \
                                                    OFFSETOF_VAR(var, member)); \
                    var; \
                    var = hashmap_iter_next_entry_offset(iter, \
                                                    OFFSETOF_VAR(var, member)))

We can work around this by making sure that var has _some_ value when OFFSETOF_VAR() is called. Strictly speaking, it invites undefined behaviour to use NULL here if we end up with pointer comparison, but MSVC runtime seems to be happy with it, and most other systems have typeof() and don't even need pointer comparison fallback code.

---
 hashmap.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git c/hashmap.h w/hashmap.h
index ef220de4c6..b011b394fe 100644
--- c/hashmap.h
+++ w/hashmap.h
@@ -449,7 +449,8 @@ static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map,
  * containing a @member which is a "struct hashmap_entry"
  */
 #define hashmap_for_each_entry(map, iter, var, member) \
-	for (var = hashmap_iter_first_entry_offset(map, iter, \
+	for (var = NULL, /* for systems without typeof */ \
+	     var = hashmap_iter_first_entry_offset(map, iter, \
 						OFFSETOF_VAR(var, member)); \
 		var; \
 		var = hashmap_iter_next_entry_offset(iter, \
Previous: Junio C HamanoNext: Johannes Schindelin
Message 26 of 70 in “CMake and Visual Studio”
  1. 00/10 CMake and Visual StudioJohannes Schindelin via GitGitGadget, Sep 25, 2020
  2. 01/10 cmake: ignore files generated by CMake as run in Visual StudioJohannes Schindelin via GitGitGadget, Sep 25, 2020
  3. 02/10 cmake: do find Git for Windows' shell interpreterJohannes Schindelin via GitGitGadget, Sep 25, 2020
  4. Sibi SiddharthanSep 25, 2020
  5. Johannes SchindelinSep 26, 2020
  6. Đoàn Trần Công DanhSep 27, 2020
  7. Johannes SchindelinSep 28, 2020
  8. Đoàn Trần Công DanhSep 29, 2020
  9. Johannes SchindelinSep 29, 2020
  10. 03/10 cmake: ensure that the `vcpkg` packages are found on WindowsJohannes Schindelin via GitGitGadget, Sep 25, 2020
  11. 07/10 cmake (Windows): complain when encountering an unknown compilerJohannes Schindelin via GitGitGadget, Sep 25, 2020
  12. Sibi SiddharthanSep 25, 2020
  13. Johannes SchindelinSep 26, 2020
  14. 06/10 cmake (Windows): let the `.dll` files are found when running the testsJohannes Schindelin via GitGitGadget, Sep 25, 2020
  15. Eric SunshineSep 25, 2020
  16. Johannes SchindelinSep 26, 2020
  17. 05/10 cmake: quote the path accurately when editing `test-lib.sh`Johannes Schindelin via GitGitGadget, Sep 25, 2020
  18. 08/10 cmake (Windows): initialize vcpkg/build dependencies automaticallyJohannes Schindelin via GitGitGadget, Sep 25, 2020
  19. Sibi SiddharthanSep 30, 2020
  20. Johannes SchindelinSep 30, 2020
  21. 09/10 cmake (Windows): recommend using Visual Studio's built-in CMake supportJohannes Schindelin via GitGitGadget, Sep 25, 2020
  22. Junio C HamanoSep 25, 2020
  23. Johannes SchindelinSep 26, 2020
  24. 10/10 hashmap_for_each_entry(): work around MSVC's run-time check failure #3Johannes Schindelin via GitGitGadget, Sep 25, 2020
  25. Junio C HamanoSep 25, 2020
  26. Junio C HamanoSep 26, 2020
  27. Johannes SchindelinSep 26, 2020
  28. 04/10 cmake: fall back to using `vcpkg`'s `msgfmt.exe` on WindowsJohannes Schindelin via GitGitGadget, Sep 25, 2020
  29. 00/10 CMake and Visual StudioJohannes Schindelin via GitGitGadget, Sep 26, 2020
  30. 01/10 cmake: ignore files generated by CMake as run in Visual StudioJohannes Schindelin via GitGitGadget, Sep 26, 2020
  31. 05/10 cmake: quote the path accurately when editing `test-lib.sh`Johannes Schindelin via GitGitGadget, Sep 26, 2020
  32. 03/10 cmake: ensure that the `vcpkg` packages are found on WindowsJohannes Schindelin via GitGitGadget, Sep 26, 2020
  33. 04/10 cmake: fall back to using `vcpkg`'s `msgfmt.exe` on WindowsJohannes Schindelin via GitGitGadget, Sep 26, 2020
  34. 02/10 cmake: do find Git for Windows' shell interpreterJohannes Schindelin via GitGitGadget, Sep 26, 2020
  35. Øystein WalleSep 28, 2020
  36. Johannes SchindelinSep 28, 2020
  37. 10/10 hashmap_for_each_entry(): workaround MSVC's runtime check failure #3Junio C Hamano via GitGitGadget, Sep 26, 2020
  38. 09/10 cmake (Windows): recommend using Visual Studio's built-in CMake supportJohannes Schindelin via GitGitGadget, Sep 26, 2020
  39. 08/10 cmake (Windows): initialize vcpkg/build dependencies automaticallyJohannes Schindelin via GitGitGadget, Sep 26, 2020
  40. 06/10 cmake (Windows): let the `.dll` files be found when running the testsJohannes Schindelin via GitGitGadget, Sep 26, 2020
  41. 07/10 cmake (Windows): complain when encountering an unknown compilerJohannes Schindelin via GitGitGadget, Sep 26, 2020
  42. 00/11 CMake and Visual StudioJohannes Schindelin via GitGitGadget, Sep 28, 2020
  43. 01/11 cmake: ignore files generated by CMake as run in Visual StudioJohannes Schindelin via GitGitGadget, Sep 28, 2020
  44. 03/11 cmake: ensure that the `vcpkg` packages are found on WindowsJohannes Schindelin via GitGitGadget, Sep 28, 2020
  45. 02/11 cmake: do find Git for Windows' shell interpreterJohannes Schindelin via GitGitGadget, Sep 28, 2020
  46. 05/11 cmake: quote the path accurately when editing `test-lib.sh`Johannes Schindelin via GitGitGadget, Sep 28, 2020
  47. 04/11 cmake: fall back to using `vcpkg`'s `msgfmt.exe` on WindowsJohannes Schindelin via GitGitGadget, Sep 28, 2020
  48. 09/11 cmake (Windows): recommend using Visual Studio's built-in CMake supportJohannes Schindelin via GitGitGadget, Sep 28, 2020
  49. 10/11 hashmap_for_each_entry(): workaround MSVC's runtime check failure #3Junio C Hamano via GitGitGadget, Sep 28, 2020
  50. 06/11 cmake (Windows): let the `.dll` files be found when running the testsJohannes Schindelin via GitGitGadget, Sep 28, 2020
  51. 07/11 cmake (Windows): complain when encountering an unknown compilerJohannes Schindelin via GitGitGadget, Sep 28, 2020
  52. 11/11 cmake: fix typo in message when `msgfmt` was not foundJohannes Schindelin via GitGitGadget, Sep 28, 2020
  53. Junio C HamanoSep 28, 2020
  54. Johannes SchindelinSep 29, 2020
  55. 08/11 cmake (Windows): initialize vcpkg/build dependencies automaticallyJohannes Schindelin via GitGitGadget, Sep 28, 2020
  56. Sibi SiddharthanSep 29, 2020
  57. Johannes SchindelinSep 29, 2020
  58. 00/10 CMake and Visual StudioJohannes Schindelin via GitGitGadget, Sep 30, 2020
  59. 05/10 cmake: quote the path accurately when editing `test-lib.sh`Johannes Schindelin via GitGitGadget, Sep 30, 2020
  60. 04/10 cmake: fall back to using `vcpkg`'s `msgfmt.exe` on WindowsJohannes Schindelin via GitGitGadget, Sep 30, 2020
  61. 06/10 cmake (Windows): let the `.dll` files be found when running the testsJohannes Schindelin via GitGitGadget, Sep 30, 2020
  62. 07/10 cmake (Windows): complain when encountering an unknown compilerJohannes Schindelin via GitGitGadget, Sep 30, 2020
  63. 08/10 cmake (Windows): initialize vcpkg/build dependencies automaticallyJohannes Schindelin via GitGitGadget, Sep 30, 2020
  64. Johannes SchindelinSep 30, 2020
  65. Junio C HamanoSep 30, 2020
  66. 09/10 cmake (Windows): recommend using Visual Studio's built-in CMake supportJohannes Schindelin via GitGitGadget, Sep 30, 2020
  67. 10/10 hashmap_for_each_entry(): workaround MSVC's runtime check failure #3Junio C Hamano via GitGitGadget, Sep 30, 2020
  68. 02/10 cmake: do find Git for Windows' shell interpreterJohannes Schindelin via GitGitGadget, Sep 30, 2020
  69. 03/10 cmake: ensure that the `vcpkg` packages are found on WindowsJohannes Schindelin via GitGitGadget, Sep 30, 2020
  70. 01/10 cmake: ignore files generated by CMake as run in Visual StudioJohannes Schindelin via GitGitGadget, Sep 30, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.