10000 Attempt to fix CID 530895 by cmb69 · Pull Request #949 · libgd/libgd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Attempt to fix CID 530895 #949

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Attempt to fix CID 530895 #949

wants to merge 1 commit into from

Conversation

cmb69
Copy link
Member
@cmb69 cmb69 commented Jan 18, 2025

If gaussian_coeffs() was called with a non-positve radius, bad things could happen, but we don't that; we add an assert() as safety net, and to enlighten static analyzers.


Frankly, I'm not sure what exactly is CoverityScan complaining about when they compain that sum might be zero, but this pre-condition assert() seems to be a good idea anyway.

We should probably also make sure that we don't call gaussian_coeffs() with a radius > INT_MAX / 2 - 1 (or something like that; possibly using overflow2()).

If `gaussian_coeffs()` was called with a non-positve `radius`, bad
things could happen, but we don't that; we add an `assert()` as safety
net, and to enlighten static analyzers.
@@ -842,6 +842,8 @@ gaussian_coeffs(int radius, double sigmaArg) {
double sum = 0;
int x, n, count;

assert(radius >= 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should never use assert() in library code that users can get to. would be nice if we could add a CI check to ban it in all library code. tests & examples are fine to use assert/etc... of course.

we should return errors back up, and if we think it's helpful (e.g. for bad API calls), log an error via gd_error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in a static function which is only called by the gdImageCopyGaussianBlurred() API which alread has:

libgd/src/gd_filter.c

Lines 1009 to 1011 in 0b8d708

if (radius < 1) {
return NULL;
}/* if */

This assert() is only a sanity check that we use the static function properly, and also a hint for static analyzers (and manual code review).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the nuance when it's "ok" to use assert and when people are just being lazy is very hard to communicate. people see it being used in some places and think it's ok in general.

since it's a static function, the compiler should inline it. and the compiler should be able to see the higher level has the same check and elide one of them. a quick check over here suggests it does, so the generated code is the same.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, assert() is a development tool. Release builds should always define NDEBUG, so asserts are a no-op (in my opinion, it would be better to have that the other way round: need to define DEBUG to enable asserts). We doing this:

libgd/src/gd_filter.c

Lines 23 to 28 in 0b8d708

#undef NDEBUG
/* Comment out this line to enable asserts.
* TODO: This logic really belongs in cmake and configure.
*/
#define NDEBUG 1
#include <assert.h>

(and yes, I agree that should be moved to configure)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enabling extra debug in a library should not cause it to abort and kill a process. assert usage in programs is different from libraries. we should have no asserts in the gd library regardless of its compilation mode. the existing asserts are "ok" in the sense that they always force NDEBUG which means the calls are empty. interesting that coverity accepts that behavior ... I can see both sides there.

7224

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.

2 participants
0