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

Re: [PATCH v4 2/2] ci: compile "linux-gcc-default" job with -Og

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 12, 2024, 22:11 UTC
Message-ID
<xmqqo785olpp.fsf@gitster.g>
In-Reply-To
<xmqqed946auc.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
Show 28 quoted lines
> We can squelch the warning by initializing the variable to a
> meaningless value, like 0, but I consider that such a change makes
> the resulting code a lot worse than the original, and that is not
> because I do not want an extra and meaningless assignment emitted
> [*2*] in the resulting binary whose performance impact cannot be
> measured by anybody.
>
> It is bad because it defeats efforts by compilers that understand
> the data flow a bit better to diagnose a true "used uninitialized"
> bugs we may introduce in the future.  Adding such a workaround goes
> against the longer-term health of the codebase.
>
> For example, I could add another use of mtimes_size that is not
> guarded by "if (data)" right next to it, i.e.
>
>         @@ -87,6 +87,7 @@ static int load_pack_mtimes_file(char *mtimes_file,
>                 if (ret) {
>                         if (data)
>                                 munmap(data, mtimes_size);
>         +		printf("debug %d\n", (int)mtimes_size);
>                 } else {
>                         *len_p = mtimes_size;
>                         *data_p = data;
>
> gcc-13 does notice that we are now truly using the variable
> uninitialized and flags it for us (gcc-12 also does notice but the
> important difference is that gcc-13 refrained from warning at the
> safe use of the variable that is guarded by "if (data)").

By the way, I do not know if any compiler gives us such a feature, but if the trick to squelch a false positive were finer grained, I would have been much more receptive to the idea of building with different optimization level, allowing a bit more false positives.

The workaround everybody jumps at is to initialize the variable to a meaningless value (like 0) and I have explained why it is suboptimal already. But if we can tell less intelligent compilers "we know our use of this variable AT THIS POINT is safe", e.g. by annotating the above snippet of the code, perhaps like this:

                if (ret) {
                        if (data)
				/* -Wno-uninitialized (mtimes_size) */
                                munmap(data, mtimes_size);
			printf("debug %d\n", (int)mtimes_size);

then it would be clear to the compiler that understand the annotation that inside that "if (data)" block, we know that the use of mtimes_size is not using an uninitialized variable.

For the use of the same variable on the next "debug" line, because it is outside of that "if (data)" block, the annotation should have no effect, and the compiler is free to do its own analysis and we will accept if it finds mtimes_size can be used uninitialized there. Any new use added for the same variable will not be masked by a meaningless initialization if we can use such a "workaround" to squelch false positives.

Previous: Junio C HamanoNext: Jeff King
Message 46 of 48 in “ci: detect more warnings via `-Og`”
  1. 0/2 ci: detect more warnings via `-Og`Patrick Steinhardt, Jun 6, 2024
  2. 1/2 ci: fix check for Ubuntu 20.04Patrick Steinhardt, Jun 6, 2024
  3. Jeff KingJun 6, 2024
  4. Patrick SteinhardtJun 6, 2024
  5. 2/2 ci: let pedantic job compile with -OgPatrick Steinhardt, Jun 6, 2024
  6. Jeff KingJun 6, 2024
  7. Patrick SteinhardtJun 6, 2024
  8. Jeff KingJun 6, 2024
  9. Patrick SteinhardtJun 6, 2024
  10. 0/2 ci: detect more warnings via `-Og`Patrick Steinhardt, Jun 6, 2024
  11. 1/2 ci: fix check for Ubuntu 20.04Patrick Steinhardt, Jun 6, 2024
  12. 2/2 ci: compile "linux-gcc-default" job with -OgPatrick Steinhardt, Jun 6, 2024
  13. Justin ToblerJun 6, 2024
  14. Junio C HamanoJun 6, 2024
  15. Patrick SteinhardtJun 7, 2024
  16. Junio C HamanoJun 7, 2024
  17. Jeff KingJun 8, 2024
  18. Junio C HamanoJun 7, 2024
  19. Junio C HamanoJun 7, 2024
  20. 0/4 ci: detect more warnings via `-Og`Patrick Steinhardt, Jun 7, 2024
  21. 1/4 ci: fix check for Ubuntu 20.04Patrick Steinhardt, Jun 7, 2024
  22. 2/4 Makefile: add ability to append to CFLAGS and LDFLAGSPatrick Steinhardt, Jun 7, 2024
  23. Jeff KingJun 8, 2024
  24. Junio C HamanoJun 8, 2024
  25. Patrick SteinhardtJun 10, 2024
  26. 3/4 ci: compile code with V=1Patrick Steinhardt, Jun 7, 2024
  27. 4/4 ci: compile "linux-gcc-default" job with -OgPatrick Steinhardt, Jun 7, 2024
  28. Junio C HamanoJun 7, 2024
  29. Jeff KingJun 8, 2024
  30. Junio C HamanoJun 8, 2024
  31. Patrick SteinhardtJun 10, 2024
  32. Junio C HamanoJun 6, 2024
  33. Patrick SteinhardtJun 7, 2024
  34. Junio C HamanoJun 7, 2024
  35. 0/2 ci: detect more warnings via `-Og`Patrick Steinhardt, Jun 10, 2024
  36. 1/2 Makefile: add ability to append to CFLAGS and LDFLAGSPatrick Steinhardt, Jun 10, 2024
  37. 2/2 ci: compile "linux-gcc-default" job with -OgPatrick Steinhardt, Jun 10, 2024
  38. Junio C HamanoJun 10, 2024
  39. 1/2 DONTAPPLY: -Og fallout workaroundJunio C Hamano, Jun 10, 2024
  40. Junio C HamanoJun 10, 2024
  41. Patrick SteinhardtJun 11, 2024
  42. Junio C HamanoJun 11, 2024
  43. Patrick SteinhardtJun 12, 2024
  44. Patrick SteinhardtJun 12, 2024
  45. 2/2 DONTAPPLY: -Os fallout workaroundJunio C Hamano, Jun 10, 2024
  46. Junio C HamanoJun 12, 2024
  47. Jeff KingJun 13, 2024
  48. Junio C HamanoJun 13, 2024

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.