Thanks to visit codestin.com
Credit goes to github.com

Skip to content

use giterr_set_str() wherever possible #4009

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 1 commit into from
Nov 17, 2016
Merged

use giterr_set_str() wherever possible #4009

merged 1 commit into from
Nov 17, 2016

Conversation

pranitbauva1997
Copy link
Contributor

@pranitbauva1997 pranitbauva1997 commented Nov 16, 2016

giterr_set() is used when it is required to format a string, and since
we don't really require it for this case, it is better to stick to
giterr_set_str().

This also suppresses a warning(-Wformat-security) raised by the compiler.

@ethomson
Copy link
Member

I'm not sure that I fully understand your comment - create_index_error does pass a const char * for example. That aside, this is a good fix since treating strings that contain an error message as a format string is definitely incorrect. (If the error message was %s, obviously this would be a bummer.)

@carlosmn
Copy link
Member

What compiler are you using? We do run with -Wall by default, and I haven't seen such warnings. As a general rule, C does not complain when you add constness to a variable.

In the places were we don't expect to perform any substitution, we should rather be using giterr_set_str() which doesn't involve any parsing machinery.

@@ -2358,7 +2358,7 @@ static size_t read_entry(
entry.path = (char *)path_ptr;
} else {
size_t varint_len;
size_t shared = git_decode_varint((const unsigned char *)path_ptr,
size_t shared = git_decode_varint((const unsigned char *)path_ptr,
Copy link
Member

Choose a reason for hiding this comment

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

Let's not do unrelated whitespace changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! My editor corrected it by default.

@pranitbauva1997
Copy link
Contributor Author

@ethomson I was unsure about it. But since that warning only occurs when the above-specified thing happens, I assumed it to be some technicality within variable arguments (I am not sure about this). I guess it is better to remove it altogether.

@pranitbauva1997
Copy link
Contributor Author

@carlosmn There seems to be a problem with both of our local builds I guess because after I pushed this out, I was checking out the travis-ci builds for master as well as for this PR where you can easily see that it is displaying 3 warnings in which this is one of them.
I did not add an extra "-Wall flag".

Also, I am not getting the other two warnings locally which are clearly shown in the master build on travis-ci.

@pranitbauva1997
Copy link
Contributor Author

@ethomson @carlosmn I think using giterr_set_str would be a better way out! Thanks!

`giterr_set()` is used when it is required to format a string, and since
we don't really require it for this case, it is better to stick to
`giterr_set_str()`.

This also suppresses a warning(-Wformat-security) raised by the compiler.

Signed-off-by: Pranit Bauva <[email protected]>
@pranitbauva1997 pranitbauva1997 changed the title use const char * because giterr_set() expects it use giterr_set_str() wherever possible Nov 16, 2016
@pranitbauva1997
Copy link
Contributor Author

@carlosmn @ethomson I have updated the PR.

@carlosmn
Copy link
Member

The warning I see on that build is

/home/travis/build/libgit2/libgit2/src/index.c:555:2: warning: format not a string literal and no format arguments [-Wformat-security]

which is the issue with formatting that @ethomson mentioned, it's nothing to do with const.

Ubuntu used to harden their GCC and the version string on Travis suggests they're still adding patches which is why I don't see this on my Debian GCC.

@carlosmn
Copy link
Member

The warning is likely a result of #4002 which told the compiler that we accept format strings. @pks-t fixed a lot of warnings, but presumably his GCC doesn't contain -Wformat-security in the "all" flags either.

@pranitbauva1997
Copy link
Contributor Author

@carlosmn Thanks for explaining! I was under the impression that it would only occur because of const things.

@carlosmn carlosmn merged commit 6b0510e into libgit2:master Nov 17, 2016
@pks-t
Copy link
Member

pks-t commented Nov 18, 2016

I've got a hardened gcc, as well, but it in fact does not contain "-Wformat-security".

Which makes me think - how about adding a build job that compiles with "-Werror" so that we catch compilation warnings early on? Oh wait, there are these curl warnings which would cause the build to fail... sucks :/

@carlosmn
Copy link
Member

carlosmn commented Nov 18, 2016

That curl weirdness is disappointing, but I'd like to get rid of curl regardless. We can certainly add a few choice options like -Wformat-security to the block of add-if-supported like we do for -Wdocumentation.

@pks-t
Copy link
Member

pks-t commented Nov 18, 2016

Makes sense to me, yes

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.

4 participants