Bug 4739 - session: remove duplicate input checks from session_write_buf
Summary: session: remove duplicate input checks from session_write_buf
Status: NEW
Alias: None
Product: Claws Mail
Classification: Unclassified
Component: Other (show other bugs)
Version: 4.3.0
Hardware: PC All
: P3 normal
Assignee: users
URL:
Depends on:
Blocks:
 
Reported: 2024-01-13 13:39 UTC by olaf
Modified: 2024-02-01 11:04 UTC (History)
0 users

See Also:


Attachments
gtk2-session-remove-duplicate-input-checks-from-session_w.patch (997 bytes, patch)
2024-01-13 13:39 UTC, olaf
Details | Diff
master-session-remove-duplicate-input-checks-from-session_w.patch (997 bytes, patch)
2024-01-13 13:40 UTC, olaf
Details | Diff

Description olaf 2024-01-13 13:39:55 UTC
Created attachment 2402 [details]
gtk2-session-remove-duplicate-input-checks-from-session_w.patch

The caller already did the same checks.
Comment 1 olaf 2024-01-13 13:40:18 UTC
Created attachment 2403 [details]
master-session-remove-duplicate-input-checks-from-session_w.patch
Comment 2 Paul 2024-01-13 13:49:54 UTC
This is also true of session_write_data()
Comment 3 djh 2024-01-13 16:07:31 UTC
"The caller already did the same checks."

I believe it is normal good practice for a function to check its own inputs rather than rely on callers passing exactly the right values. So if anything it should be the checks in the caller that are removed, but maybe they are needed for other reasons, I haven't checked.
Comment 4 Paul 2024-01-13 16:45:26 UTC
I agree with comment #3.
Comment 5 olaf 2024-01-14 14:37:15 UTC
(In reply to djh from comment #3)

I disagree with that. There is a reason strlen(NULL) crashes. Drepper has a good writing somewhere, which essentially says "always know the state of your data".
Which in this case means the caller is responsible.

It is true that during development such checks will be sprinkled all over the code, especially when the API evolves to something different than initially envisioned. Once the API is done, and hopefully properly documented, these superfluous checks need to be removed. They just distract the reader. Like in this case.

One may argue that in this specific case the actual checks can remain in session_write_buf because the function uses these values, and should be removed from the caller instead, because it does nothing with them. Either way is fine with me.

One may argue further that checks in both functions could be removed. The checks are always true. But it needs a separate audit/patch/bug, if session_write_msg_cb is really called again if it returned FALSE.
Comment 6 djh 2024-01-14 17:15:24 UTC
If you provide a reference to whatever Drepper wrote, I'll be happy to read it. Otherwise it's just what you claim. I would argue that "always know the state of your data" is in full agreement with what I wrote.

Generally it is worth being able to identify where a fault has occurred, even in the event that code changes. So if somebody writes some new code that calls the function then having the checks in place means that an error will be detected. Such checks are more important than optimising speed or space in most cases. Assertions can sometimes be used to achieve the best on both axes.

I don't find boiler plate code like this distracting in general.

Note You need to log in before you can comment on or make changes to this bug.