c - Void wrapped function in journalctl source -
i browsing through source code of journalctl , found piece of code not clear me.
the part talking right here: https://github.com/systemd/systemd/blob/9e83569d8ff219730912ecac441843b9531b079c/src/shared/logs-show.c#l1056
seems want avaoid multiple checks this
r = sd_journal_add_match(...); if (r < 0) { //some error } by || outputs together.
wouldn't possible doing this:
r = sd_journal_add_match(...); r |= sd_journal_add_match(...); ... why wrap output in
(void) ( ... ); ?
thx in advance.
first,
r |= sd_journal_add_match(...); is not same as
(r = sd_journal_add_match(j, "_uid=0", 0)) || (r = sd_journal_add_match(j, m2, 0)) || the |= operator bit-wise or operator assignment (same r = r | sd_()), || logical or operator. sd_jourcal_ function executed until 1 of them returns non-zero value. r contains these non-zero value. called short-circuitevaluation.
second, guess same reason why (should) cast return value of function void if not interested in use them. example:
(void)printf("hello world"); this communicates printf() returns value, not check them. should check return value:
if (printf("hello world") < 0) { // error handling here } simply writing
printf("hello world"); ignores return value silently, bad idea. guess, there tools out there (like lint) critical software, can check this.
for example provided, result of || chain casted void. without cast, code looks strange, because there value calculated, not assigned. similar this
f() || g(); // better (void)(f() || g());
Comments
Post a Comment