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

fix extra warnings when building with -Wwrite-strings #5096

Merged
merged 13 commits into from
Oct 27, 2024

Conversation

elliefm
Copy link
Contributor

@elliefm elliefm commented Oct 18, 2024

This PR fixes all the new warnings that appear when compiling with the -Wwrite-strings C dialect option (in which string literals have type const char[] rather than char[] that will crash if you modify it). This option is nice because it means the compiler's usual discarded-qualifiers warnings will now also protect us from doing dodgy stuff with string literals (unless we silence the warning with an explicit cast).

It also adds this option to CFLAGS in tools/build-with-cyruslibs.sh, which means once this is merged, all our Github CI runs will be built with this option, and will yell at us if we do dodgy stuff without at least a cast to shut it up.

There were lots of very similar fixes, which I've grouped up approximately one commit per type of fix. I suggest reviewing it commit by commit. You might also want to add -Wwrite-strings to your own local build options after this lands.

@elliefm elliefm force-pushed the v311/wwrite-strings branch 2 times, most recently from cdf4429 to 45d5e82 Compare October 21, 2024 04:31
@dilyanpalauzov
Copy link
Contributor

Why not changing:

--- a/imap/http_dav.h
+++ b/imap/http_dav.h
@@ -324,7 +324,7 @@ struct prop_entry {
     int (*put)(xmlNodePtr prop,         /* Callback to write property */
                unsigned set, struct proppatch_ctx *pctx,
                struct propstat *propstat, void *rock);
-    void *rock;                         /* Add'l data to pass to callback */
+    const void *rock;                   /* Add'l data to pass to callback */
 };
 
 /* Bitmask of property flags */

and not modifying http_caldav.c?

@elliefm
Copy link
Contributor Author

elliefm commented Oct 23, 2024

Why not changing:

--- a/imap/http_dav.h
+++ b/imap/http_dav.h
@@ -324,7 +324,7 @@ struct prop_entry {
     int (*put)(xmlNodePtr prop,         /* Callback to write property */
                unsigned set, struct proppatch_ctx *pctx,
                struct propstat *propstat, void *rock);
-    void *rock;                         /* Add'l data to pass to callback */
+    const void *rock;                   /* Add'l data to pass to callback */
 };
 
 /* Bitmask of property flags */

and not modifying http_caldav.c?

Because it's not always const for all callbacks that use it. e.g. for "supported-method-set", this field is a pointer to another callback function.

@elliefm elliefm marked this pull request as ready for review October 23, 2024 03:21
@rsto
Copy link
Member

rsto commented Oct 23, 2024

will now also protect us from doing dodgy stuff with string literals (unless we silence the warning with an explicit cast).

I see that the PR does explicitly cast away the const from some string literals. But doesn't this indicate that either the API expecting a non-const is broken, or passing the string literal is wrong/dangerous?

@elliefm
Copy link
Contributor Author

elliefm commented Oct 24, 2024

will now also protect us from doing dodgy stuff with string literals (unless we silence the warning with an explicit cast).

I see that the PR does explicitly cast away the const from some string literals. But doesn't this indicate that either the API expecting a non-const is broken, or passing the string literal is wrong/dangerous?

Yep. In the cases where I just shut it up with a cast, it was because it didn't seem worth the effort (right now) to fix the API or implementation in question. We can be reasonably assured that these specific cases are safe, since we aren't seeing the kind of crashes that would occur if they weren't. A lot of this this stuff isn't well tested, so even though I could potentially do some refactoring to handle the strings more sanely, it would be hard to prove that my refactoring hadn't introduced other bugs. Shutting up the warnings in these cases will let us collectively enable -Wwrite-strings sooner, and -Wwrite-strings will prevent us creating similar problems in new code and APIs, where we won't already know from experience that it's safe.

But I do think it would be good to fix those APIs and implementations properly! Ideally, we'd never ever need to cast away const from a string literal. But on balance, I think getting to the point where we can always use -Wwrite-strings is the higher priority. And that will help us get it right when we do eventually get around to fixing the other bits.

@dilyanpalauzov
Copy link
Contributor

When I asked here why the interfaces are not changed to require const, the reply was that this will contradict program logic. When Robert raised the same question, the answer was different — this is not a priority.

Copy link
Member

@rsto rsto left a comment

Choose a reason for hiding this comment

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

Agreed. LGTM.

@elliefm
Copy link
Contributor Author

elliefm commented Oct 27, 2024

When I asked here why the interfaces are not changed to require const, the reply was that this will contradict program logic. When Robert raised the same question, the answer was different — this is not a priority.

I explains the casts you each asked about; they were different casts, and had different explanations.

@elliefm elliefm merged commit 0ddd4ab into cyrusimap:master Oct 27, 2024
1 check passed
@elliefm elliefm deleted the v311/wwrite-strings branch October 27, 2024 22:20
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