Skip to content

Add library attribute for no-failure allocators #7390

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

Merged
merged 2 commits into from
Apr 3, 2025

Conversation

b4n
Copy link
Contributor

@b4n b4n commented Mar 21, 2025

For example GLib and GTK abort on most allocation failures, making the nullPointerOutOfMemory check produce lots of false positives.

To fix this, introduce a new allocator attribute "no-fail" to notify that this allocator cannot fail and that this check is thus irrelevant for it.

For example GLib and GTK abort on most allocation failures, making the
nullPointerOutOfMemory check produce lots of false positives.

To fix this, introduce a new allocator attribute "no-fail" to notify
that this allocator cannot fail and that this check is thus irrelevant
for it.
@b4n
Copy link
Contributor Author

b4n commented Mar 21, 2025

There probably are other options for tagging the library functions, I can update if wanted.

@firewave
Copy link
Collaborator

This also needs to be added in the Library Editor in the GUI (see gui/cppchecklibrarydata.cpp) as well as the GUI tests in gui/test/cppchecklibrarydata (those should fail without the editor integration).

@firewave
Copy link
Collaborator

Could you also provide a link to documentation that supports this behavior?

@b4n
Copy link
Contributor Author

b4n commented Mar 21, 2025

Could you also provide a link to documentation that supports this behavior?

e.g. https://docs.gtk.org/glib/func.malloc.html:

If the allocation fails (because the system is out of memory), the program is terminated.

The idea behind this is that allocation failure for most trivial allocations are hard to handle well, and likely will end up terminating the program anyway. More classic allocation is available with e.g. g_try_malloc() when ready to deal with potential allocation issues (usually targeted at larger or optional allocations).

@firewave
Copy link
Collaborator

Thanks.

Allocates n_bytes bytes of memory. If n_bytes is 0 it returns NULL.

That indicates that some code still needs the check afterwards. So it might be worth reviewing the calls anyways.

The idea behind this is that allocation failure for most trivial allocations are hard to handle well, and likely will end up terminating the program anyway.

Yes, OOM handling/recovery is very hard to impossible.
Everything major I worked on would just trigger an immediate shutdown in that case. On some systems that would never happen though because the OOM Killer was over-eagerly terminating the process in memory pressure situations.

@b4n
Copy link
Contributor Author

b4n commented Mar 21, 2025

Allocates n_bytes bytes of memory. If n_bytes is 0 it returns NULL.

That indicates that some code still needs the check afterwards. So it might be worth reviewing the calls anyways.

Yes, in some cases NULL is still a possible result, while not being an allocation failure. However, many, like g_string_new() will never return NULL because they either succeed at allocating their internal structure, or they will terminate.

The cases of e.g. g_malloc(0) or g_strdup(NULL) will indeed return NULL and might need checking, but IMO those are both rare, or other checks should be able to find issues: e.g. ptr = g_malloc(n) where n=0 should catch all access of ptr above or equal to 0 -- and thus see an out of bound access on anything.

As things are right now, most GLib-using code will just produce gazillion false-positives (see e.g. geany/geany-plugins#1417): even the few checks in gtk.cfg required overrides to pass in the first place. So even if we say that it should catch the g_malloc(0) case, ignoring it is a worthy trade-off IMO as as-is I would simply disable/ignore the check everywhere (and I don't know of a way to disable that one alone, but I might just lack configuration knowledge, I'm not (yet?) a guru user 🙂 )
However, if you can think of a more general solution that can express those specific cases, why not.

@firewave
Copy link
Collaborator

However, if you can think of a more general solution that can express those specific cases, why not.

I think this is as generic as it gets for APIs. But it is up to @danmar to decide.

Regarding cases in terms of the standard see the previous thread in #7068.

@firewave
Copy link
Collaborator

Please also add an entry with the new attribute to the GUI test data.

@b4n
Copy link
Contributor Author

b4n commented Mar 22, 2025

@firewave you mean setting it to true in one of the tests, or is there another place I missed?

@firewave
Copy link
Collaborator

@firewave you mean setting it to true in one of the tests, or is there another place I missed?

Yes. I think that would be an additional entry in gui/test/cppchecklibrarydata/files/memory_resource_valid.cfg.

@b4n b4n force-pushed the allow-no-fail branch 2 times, most recently from 518a421 to 068929a Compare March 22, 2025 21:49
b4n added a commit to b4n/geany-plugins that referenced this pull request Mar 23, 2025
This produces too many false-positives with GLib code.

See danmar/cppcheck#7068 and
danmar/cppcheck#7390.

Closes geany#1417.
@danmar danmar merged commit 5a2f12f into danmar:main Apr 3, 2025
60 checks passed
@danmar
Copy link
Owner

danmar commented Apr 3, 2025

@b4n thanks for your contribution.

@danmar
Copy link
Owner

danmar commented Apr 3, 2025

The name "no-fail" is not ideal. But I don't have better suggestion directly. Something like returns-nonnull maybe.. We can switch relatively easy in the near future if we want.

@firewave
Copy link
Collaborator

firewave commented Apr 3, 2025

We should still file a ticket about the false positive so this is documented.

I also think it might be missing in the library data editor (but that should have failed a test!?).

@b4n
Copy link
Contributor Author

b4n commented Apr 3, 2025

@danmar it isn't, but returns-nonnull is not great either because as mentioned in #7390 (comment) some specific calls like g_malloc(0) can return NULL, it's just never the result of an allocation failure but the API contract.
But I agree that the name isn't perfect, if anybody can come up with a better one soon enough it's indeed welcome.

@b4n
Copy link
Contributor Author

b4n commented Apr 3, 2025

@firewave didn't I fix that in the latest revision? There's no UI for it sure, but it's not the only option that is not available there I believe, and it should be loaded/saved properly now.

@firewave
Copy link
Collaborator

firewave commented Apr 3, 2025

didn't I fix that in the latest revision? There's no UI for it sure, but it's not the only option that is not available there I believe, and it should be loaded/saved properly now.

Ah, it is being loaded but not displayed. The latter can obviously not being tested easily. Guess we need to review the UI and file a ticket about it.

@danmar
Copy link
Owner

danmar commented Apr 3, 2025

We should still file a ticket about the false positive so this is documented.

Oops!! I will create a ticket! I really shouldn't have merged this without a ticket :-(

Guess we need to review the UI and file a ticket about it.

I am not against it however..
My feeling is that the UI is not very nice and user friendly :-(
I have no data at all but I don't think that people use it currently.
Will anybody complain if we remove it rather than maintaining it..

@firewave
Copy link
Collaborator

firewave commented Apr 4, 2025

Oops!! I will create a ticket! I really shouldn't have merged this without a ticket :-(

It happens.

My feeling is that the UI is not very nice and user friendly :-(

It beats editing a raw XML file.

I have no data at all but I don't think that people use it currently.
Will anybody complain if we remove it rather than maintaining it..

I assume a few people actually use - otherwise we wouldn't have gotten several PRs extending it in the past.

I think we can keep it because it is very static in nature so there is not much in terms of support.

@firewave
Copy link
Collaborator

firewave commented Apr 9, 2025

The memory functions have no UI mask at all so there was nothing to add here. Having support for that is covered by https://trac.cppcheck.net/ticket/10688.

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.

3 participants