Skip to content

Some warnings#4784

Merged
pks-t merged 2 commits intolibgit2:masterfrom
tiennou:fix/warnings
Sep 28, 2018
Merged

Some warnings#4784
pks-t merged 2 commits intolibgit2:masterfrom
tiennou:fix/warnings

Conversation

@tiennou
Copy link
Copy Markdown
Contributor

@tiennou tiennou commented Aug 26, 2018

That's the 2 warnings I know of I've hinted at recently.

Comment thread src/path.c Outdated
size_t filelen;

if (gitfile < 0 && gitfile >= ARRAY_SIZE(gitfiles)) {
if (gitfile >= ARRAY_SIZE(gitfiles)) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm doubtful of this fix, but as far as I can see, checking enums for validity has always been a perilous business. The alternative is to cast before checking, but it gets messy because the signedness of an enum is compiler-dependent. Since I believe this argument is never "user-controlled", I'm tempted to remove the "useless" test.

Copy link
Copy Markdown
Member

@carlosmn carlosmn Aug 28, 2018

Choose a reason for hiding this comment

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

What is the specific warning here? This looks like the error is that this should be || instead of && as a a < 0 ∧ a > b where b > 0 is never going to be true.

Copy link
Copy Markdown
Contributor Author

@tiennou tiennou Aug 28, 2018

Choose a reason for hiding this comment

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

…/libgit2/src/path.c:1890:14: Comparison of unsigned enum expression < 0 is always false

Changing to || doesn't fix the warning, but I can do the following, which does :

if ((signed int)gitfile < 0 || gitfile >= ARRAY_SIZE(gitfiles))

and I think gitfile >= ARRAY_SIZE(gitfile) would have caught it, since it now looks like a very big unsigned int.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hm right, this only seems to be an older clang thing. I don't know what its version 900 corresponds to in "real" clang versions, but on Debian I see this with clang 5 but not with 6. I don't know if they've decided to change the signedness of enums, but this is up to the particular implementation so removing this check would break it for some (apparently including later clang versions).

Copy link
Copy Markdown
Member

@pks-t pks-t Aug 30, 2018

Choose a reason for hiding this comment

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

I'd be fine with just removing the check altogether. As @tiennou said, this function is not exposed, and all of our internal callers make sure to just pass proper enum values.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.7.0

included in Xcode 9.4.1 (which doesn't feel that old). I'm 👍 with removing the check, because I don't think there's a clean way to do it otherwise.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's internal now, but it is useful outside of the library and when we expose it we'd have to content with random inputs (I've been meaning to expose this for a while as the fork I send to production has this as public).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

	if (!(gitfile > 0 && gitfile <= ARRAY_SIZE(gitfiles))) {

does not produce the warning for me in clang 5 and it also fixes the logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I've used this, fixed up, and rebased.

Comment thread src/path.c Outdated
"warning: values of type 'OSStatus' should not be used as format arguments; add an explicit cast to 'int' instead [-Wformat]"
@tiennou
Copy link
Copy Markdown
Contributor Author

tiennou commented Sep 25, 2018

Should be good. I ended up using the enum value for 0 explicitly, which silences the warning 🤷‍♂️.

Comment thread src/path.c
size_t filelen;

if (gitfile < 0 && gitfile >= ARRAY_SIZE(gitfiles)) {
if (!(gitfile >= GIT_PATH_GITFILE_GITIGNORE && gitfile < ARRAY_SIZE(gitfiles))) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I normally wouldn't like relying on the fact that this enum is actually the first one. But there already is a comment in "src/path.h", stating that the enum is sensitive to ordering. So this is fine

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm also quite sure there's no simple way to check for an enum signedness, without resorting to build-system shenanigans or explicitly making the enum signed. Hence the test case I've added, which should hopefully catch a change in definition.

@pks-t pks-t merged commit ba1cd49 into libgit2:master Sep 28, 2018
@pks-t
Copy link
Copy Markdown
Member

pks-t commented Sep 28, 2018

Thanks again, @tiennou!

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