0

I have been looking at the journalctl code and came across the following block of code.

It seems to be a shorthand way of exiting out of a series of condition tests if there is a non-zero result as the conditions are being tested. Pretty clever.

But I'm unsure about the purpose of the void cast. Is it to suppress some compiler output? g++ does not care either way even with -Wall and -pedantic -pedantic-errors.

m1 = strjoina("_SYSTEMD_UNIT=", unit);
m2 = strjoina("COREDUMP_UNIT=", unit);
m3 = strjoina("UNIT=", unit);
m4 = strjoina("OBJECT_SYSTEMD_UNIT=", unit);

(void)(
  /* Look for messages from the service itself */
  (r = sd_journal_add_match(j, m1, 0)) ||

  /* Look for coredumps of the service */
  (r = sd_journal_add_disjunction(j)) ||
  (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
  (r = sd_journal_add_match(j, "_UID=0", 0)) ||
  (r = sd_journal_add_match(j, m2, 0)) ||

  /* Look for messages from PID 1 about this service */
  (r = sd_journal_add_disjunction(j)) ||
  (r = sd_journal_add_match(j, "_PID=1", 0)) ||
  (r = sd_journal_add_match(j, m3, 0)) ||

  /* Look for messages from authorized daemons about this service */
  (r = sd_journal_add_disjunction(j)) ||
  (r = sd_journal_add_match(j, "_UID=0", 0)) ||
  (r = sd_journal_add_match(j, m4, 0))
);

f (r == 0 && endswith(unit, ".slice")) {
   ...
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
hookenz
  • 36,432
  • 45
  • 177
  • 286
  • 1
    Likely superstition. Or a compiler with broken warning system. – SergeyA Jul 27 '17 at 21:25
  • 2
    To make crap code even crappier? Anything that embeds strings like this "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1" is beyond awful. –  Jul 27 '17 at 21:26
  • @NeilButterworth - Somewhat agree. Who would have thought that fc2e22bc6ee647b6b90729ab34a250b1= coredump? systemd was always pretty controversial. – hookenz Jul 27 '17 at 21:32

1 Answers1

5
  1. To suppress compiler warnings about unused result of this logical expression. This logiocal-or expression is built for its side-effects and its short-circuit evaluation properties. The author of the code does not care about its final result though. The compiler might not be smart enough to realize that. It might warn about the final result being discarded.
  2. To convey to human readers the intent of the code's author to discard the result of the logical expression.

Explicit conversion to void is a generally accepted idiom that conveys that intent (to both the compiler and human readers).

P.S. This application of || is one example of branching in "classic" C-style expression programming, as I described it here: https://stackoverflow.com/a/1618867/187690

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Only in this case it is clear what this is used for both compilers and readers. – SergeyA Jul 27 '17 at 21:28
  • 1
    Yep, here it helps to understand that the point of that expression is to exploit the short circuit evaluation to have a "fallback chain" of sorts without having a ton of nested `if`s. The final result of the expression is not interesting. – Matteo Italia Jul 27 '17 at 21:30