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

libdm: fix -Wparentheses warning. #56

Closed
wants to merge 1 commit into from
Closed

libdm: fix -Wparentheses warning. #56

wants to merge 1 commit into from

Conversation

jcai19
Copy link

@jcai19 jcai19 commented Aug 5, 2021

This fixes a C operator precedence-related issue exposed by the warning.

Link:
#55

Copy link
Contributor

@kergon kergon left a comment

Choose a reason for hiding this comment

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

Did you test this? Isn't it the wrong way around? Number of policy arguments plus an extra one if the migration_threshold option will also be included, all times 2.

@@ -2489,7 +2489,7 @@ static int _cache_emit_segment_line(struct dm_task *dmt,
EMIT_PARAMS(pos, " %s", name);

/* Do not pass migration_threshold 2048 which is default */
EMIT_PARAMS(pos, " %u", (seg->policy_argc + (seg->migration_threshold != 2048) ? 1 : 0) * 2);
Copy link

Choose a reason for hiding this comment

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

should it be ((seg->migration_threshold != 2048) ? 1 : 0))

Copy link
Author

Choose a reason for hiding this comment

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

The current implementation is to appease -Wparentheses without changing program semantics, as you pointed out in #56 (comment). :)

@zkabelac
Copy link

zkabelac commented Aug 5, 2021

Yep patch looks patch - it's been nicely covering itself due to test nature

@m-gupta
Copy link

m-gupta commented Aug 5, 2021

Here is the warning log:

lvm2-2.02.187-r3: libdm-deptree.c:2467:81: warning: operator '?:' has lower precedence than '+'; '+' will be evaluated first [-Wparentheses]
lvm2-2.02.187-r3: EMIT_PARAMS(pos, " %u", (seg->policy_argc + (seg->migration_threshold != 2048) ? 1 : 0) * 2);
lvm2-2.02.187-r3: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
lvm2-2.02.187-r3: libdm-deptree.c:2037:59: note: expanded from macro 'EMIT_PARAMS'
lvm2-2.02.187-r3: if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {
lvm2-2.02.187-r3: ^~~
lvm2-2.02.187-r3: libdm-deptree.c:2467:81: note: place parentheses around the '+' expression to silence this warning
lvm2-2.02.187-r3: EMIT_PARAMS(pos, " %u", (seg->policy_argc + (seg->migration_threshold != 2048) ? 1 : 0) * 2);
lvm2-2.02.187-r3: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
lvm2-2.02.187-r3: libdm-deptree.c:2037:59: note: expanded from macro 'EMIT_PARAMS'
lvm2-2.02.187-r3: if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {
lvm2-2.02.187-r3: ^~~
lvm2-2.02.187-r3: libdm-deptree.c:2467:81: note: place parentheses around the '?:' expression to evaluate it first
lvm2-2.02.187-r3: EMIT_PARAMS(pos, " %u", (seg->policy_argc + (seg->migration_threshold != 2048) ? 1 : 0) * 2);
lvm2-2.02.187-r3: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
lvm2-2.02.187-r3: libdm-deptree.c:2037:59: note: expanded from macro 'EMIT_PARAMS'
lvm2-2.02.187-r3: if ((w = dm_snprintf(params + p, paramsize - (size_t) p, str)) < 0) {
lvm2-2.02.187-r3: ^~~
lvm2-2.02.187-r3: 1 warning generated.

Based on the warning, the existing code was already evaluating "+" before "?" because of C operator precedence.
If ? needs to be evaluated before "+", then this implies that the existing code is buggy. Happy to see clang flagging this.

@m-gupta
Copy link

m-gupta commented Aug 5, 2021

Here is a small snipppet showing the predence of "+" and "?" :

int main() {
int res = 10, cond = 1;
res = res + (cond != 1) ? 5 : 6;
printf("Expected result is 16\n");
printf("Actual %d\n", res);
}

Expected result is 16
Actual 5

So, the current code is buggy if "?" was expected to be evaluated first by the initial author.

@jcai19
Copy link
Author

jcai19 commented Aug 5, 2021

Thanks @m-gupta for the experiments. The reason I make the patch the way it is is indeed to appease the warning without changing the program semantics, since addition has higher precedence over ternary conditional. Changing it based on the comment from #56 (review) would change the program semantics, or is that expected @kergon?

@kergon
Copy link
Contributor

kergon commented Aug 6, 2021

When you get a compiler warning, the idea is not to unthinkingly suppress the warning, but to check what the code was supposed to do, understand it, and then resolve the warning accordingly.

There are two possible patches here. Look at each option in the context of the lines around it then decide which of the two is the one that makes sense.

@jcai19
Copy link
Author

jcai19 commented Aug 12, 2021

Thanks for the comment. I've updated the code accordingly.

@jcai19 jcai19 requested a review from kergon August 18, 2021 22:51
@jcai19 jcai19 changed the title libdm: appease -Wparentheses warning. libdm: fix -Wparentheses warning. Aug 18, 2021
This fixes a C operator precedence-related issue exposed by the warning.

Link:
#55
@jcai19 jcai19 closed this Sep 14, 2021
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

4 participants