Ticket #667 (closed defect: fixed)

Opened 3 weeks ago

Last modified 3 weeks ago

Undefined operations using modulo arithmetic

Reported by: baker Owned by: somebody
Priority: major Milestone: All Platforms
Component: evanstrig Version: 7.9
Keywords: sequence-point Cc:


There are several instances in Earthworm of code like this in src/seismic_processing/evanstrig/mteltrg.c that increment a value using modulo arithmetic, which generates a warning for an undefined operation:

mteltrg.c: In function ‘tel_triggered_on_channel’:
mteltrg.c:813:24: warning: operation on ‘qch->i_lp_buf’ may be undefined [-Wsequence-point]
          qch->i_lp_buf = (++qch->i_lp_buf) % Lflt_len;

This is tagged as a sequence point violation ([-Wsequence-point]).

A good explanation can be found at https://stackoverflow.com/questions/10623114/operation-on-may-be-undefined (my edits):

You're not allowed to modify the same value twice without a sequence point in between.
In this case that means you're not allowed to both increment qch->i_lp_buf [ed.] using
++ and reassign it using =.

If you just use + 1 instead of ++, it will work fine.

++ and -- have side effects, whereas an expression using + 1 or - 1 is pure, i.e., has no side effects.

Change History

comment:1 Changed 3 weeks ago by baker

  • Status changed from new to closed
  • Resolution set to fixed

Fixed in r7523.

Note: See TracTickets for help on using tickets.