Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codechange: Use access __attribute__ to silence warnings in GCC #8522

Merged
merged 1 commit into from Jan 8, 2021

Conversation

Milek7
Copy link
Contributor

@Milek7 Milek7 commented Jan 8, 2021

Motivation / Problem

GCC in some cases warns about uninitialized uses where in fact pointer is not dereferenced at all, such as last pointer in safe string functions.

/home/milek7/OpenTTD/src/network/network_content_gui.cpp: In member function ‘void NetworkContentListWindow::OpenExternalSearch()’:
/home/milek7/OpenTTD/src/network/network_content_gui.cpp:328:36: warning: ‘last’ may be used uninitialized [-Wmaybe-uninitialized]
  328 |                 char *pos = strecpy(url, "http://grfsearch.openttd.org/?", last);
      |                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/milek7/OpenTTD/src/network/core/address.h:15,
                 from /home/milek7/OpenTTD/src/network/core/tcp.h:15,
                 from /home/milek7/OpenTTD/src/network/core/tcp_content.h:16,
                 from /home/milek7/OpenTTD/src/network/network_content.h:13,
                 from /home/milek7/OpenTTD/src/network/network_content_gui.h:13,
                 from /home/milek7/OpenTTD/src/network/network_content_gui.cpp:23:
/home/milek7/OpenTTD/src/network/core/../../string_func.h:34:7: note: by argument 3 of type ‘const char*’ to ‘char* strecpy(char*, const char*, const char*)’ declared here
   34 | char *strecpy(char *dst, const char *src, const char *last);
      |       ^~~~~~~
/home/milek7/OpenTTD/src/network/network_content_gui.cpp:325:22: note: ‘url’ declared here
  325 |                 char url[1024];
      |                      ^~~

Description

Add NOACCESS macro that uses access __attribute__ to tell compiler that pointer is not dereferenced at all. https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html

Limitations

None (except ugly macro).

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@LordAro
Copy link
Member

LordAro commented Jan 8, 2021

While I don't doubt this is fine, where are you seeing these GCC warnings? I've not seen anything like them before

@Milek7
Copy link
Contributor Author

Milek7 commented Jan 8, 2021

gcc --version
gcc (GCC) 11.0.0 20200910 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

@LordAro
Copy link
Member

LordAro commented Jan 8, 2021

Ah, shiny new GCC 11 warnings

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants