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

cppcheck: fix "Suspicious condition and usage" #2755

Merged
merged 2 commits into from
Jul 25, 2016
Merged

cppcheck: fix "Suspicious condition and usage" #2755

merged 2 commits into from
Jul 25, 2016

Conversation

serval2412
Copy link
Contributor

@serval2412 serval2412 commented Jul 24, 2016

[fontforge/dumppfa.c:2370]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.
[gdraw/gtabset.c:408]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.
[fontforge/stemdb.c:4619]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
[fontforge/stemdb.c:4626]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
[fontforge/svg.c:2046]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.

[fontforge/dumppfa.c:2370]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.
[gdraw/gtabset.c:408]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.
[fontforge/stemdb.c:4619]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
[fontforge/stemdb.c:4626]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
[fontforge/svg.c:2046]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
@@ -2043,7 +2043,7 @@ static void xmlParseColorSource(xmlNodePtr top,char *name,DBounds *bbox,
*_grad = NULL; *_epat = NULL;
if ( colour_source==NULL )
LogError(_("Could not find Color Source with id %s."), name );
else if ( (islinear = xmlStrcmp(colour_source->name,(xmlChar *) "linearGradient")==0) ||
else if ( ((islinear = xmlStrcmp(colour_source->name,(xmlChar *) "linearGradient"))==0) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

If xmlStrcmp is like strcmp, then the placement of brackets here is now wrong - a zero return value from strcmp indicates a match, so comparing its return value == 0 would indicate the presence of a linear gradient. So you probably want something like

else if ((islinear = (xmlStrcmp(colour_source->name, (xmlChar *) "linearGradient") == 0))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right I suppose, I fixed this.

[fontforge/dumppfa.c:2370]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.
[gdraw/gtabset.c:408]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.
[fontforge/stemdb.c:4619]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
[fontforge/stemdb.c:4626]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
[fontforge/svg.c:2046]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
@@ -4616,14 +4616,14 @@ static void MarkDStemCorner( struct glyphdata *gd,struct pointdata *pd ) {
for ( i=0; i<pd->prevcnt && !has_stem; i++ ) {
stem = pd->prevstems[i];
if ( !stem->toobig && (
( x_dir && ( hv = IsUnitHV( &stem->unit,true ) == 1 )) ||
( x_dir && ( (hv = IsUnitHV( &stem->unit,true )) == 1 )) ||
( !x_dir && hv == 2 )))
Copy link
Contributor

Choose a reason for hiding this comment

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

The bracket fix looks fine, but looking at the statement as a whole, it seems a bit odd that IsUnitHV (and the assignment to hv) is only called after checking if x_dir is true.

I wonder if it should actually be something closer to

if ((hv = IsUnitHV(&stem->unit, true)) == 1 && x_dir) || (!x_dir && hv == 2)

I haven't studied the code long enough to know if this is appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, in C (on the contrary of C++), we don't know in which order conditions are evaluated, so I think it could be better to put "hv = IsUnitHV( &stem->unit,true )" just after "stem" assignation in both cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW, what about merging this patch as it is and making the quoted change on a second patch?
Indeed, this change isn't related to cppcheck.

Copy link
Contributor

@jtanx jtanx Jul 25, 2016

Choose a reason for hiding this comment

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

You're right, in C (on the contrary of C++), we don't know in which order conditions are evaluated,

Hmm I'm not sure about that - the rearrangement was taking into account short-circuit evaluation (which works in both c/c++). This bug also comes up on coverity (CID1309095)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, thought it wasn't sure for C.
Thank you for having merged this one!
I'm gonna create a new pull request for the change we've discussed.

@jtanx jtanx merged commit 04ff32e into fontforge:master Jul 25, 2016
Omnikron13 pushed a commit to Omnikron13/fontforge that referenced this pull request May 31, 2022
* cppcheck: fix "Suspicious condition and usage"

[fontforge/dumppfa.c:2370]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.
[gdraw/gtabset.c:408]: (warning) Suspicious usage of 'sizeof' with a numeric constant as parameter.
[fontforge/stemdb.c:4619]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
[fontforge/stemdb.c:4626]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
[fontforge/svg.c:2046]: (style) Suspicious condition (assignment + comparison); Clarify expression with parentheses.
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