Uploaded image for project: 'Embedded Software & Tools'
  1. Embedded Software & Tools
  2. EXT_EP-9945

Compiler incorrectly elides short-circuit behavior of && when right operand might invoke undefined behavior

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: High High
    • Code Generation Tools
    • CODEGEN-7956
    • Hide
      C7000_1.4.0.LTS
      C6000_8.2.0
      C6000_8.3.0
      MSP430_20.2.0.LTS
      ARM_18.12.0.LTS
      MSP430_18.12.0.LTS
      PRU_2.3.0
      ARM_20.2.0.LTS
      C2000_18.12.0.LTS
      C2000_20.2.0.LTS
      Show
      C7000_1.4.0.LTS C6000_8.2.0 C6000_8.3.0 MSP430_20.2.0.LTS ARM_18.12.0.LTS MSP430_18.12.0.LTS PRU_2.3.0 ARM_20.2.0.LTS C2000_18.12.0.LTS C2000_20.2.0.LTS
    • Hide
      ARM_18.12.8.LTS*
      MSP430_18.12.8.LTS*
      MSP430_20.2.5.LTS*
      ARM_20.2.5.LTS*
      C2000_20.2.5.LTS*
      C7000_1.4.2.LTS
      PRU_2.3.4*
      C6000_8.3.9
      C2000_18.12.8.LTS*
      Show
      ARM_18.12.8.LTS* MSP430_18.12.8.LTS* MSP430_20.2.5.LTS* ARM_20.2.5.LTS* C2000_20.2.5.LTS* C7000_1.4.2.LTS PRU_2.3.4* C6000_8.3.9 C2000_18.12.8.LTS*
    • default
    • Hide
      Make the right-hand expressions volatile, which will foil the optimization. For the following expressions, the right-hand expressions are Z and W.
      if (X) Y = Z; else Y = W;
      Y = (X ? Z : W);
      Show
      Make the right-hand expressions volatile, which will foil the optimization. For the following expressions, the right-hand expressions are Z and W. if (X) Y = Z; else Y = W; Y = (X ? Z : W);

      The optimizer incorrectly optimized an expression and moved an expression invoking undefined behavior before a test that would have prevented the expression from being executed. Specifically, the optimizer moved a null pointer dereference to a point before a test that would have guarded against the null pointer being dereferenced. The optimization in question is essentially as follows. Given

      if (X) Y = Z; else Y = W
      or
      Y = (X ? Z : W)

      where all of X, Y, Z, and W are bool or single-bit bit-fields, and one of Z or W is the constant 0, the optimizer would produce

      Y = (X&Z) | ((!X)&W)

      (The user's code might not look exactly like the above, because the optimizer can produce the above code by rearranging logically similar code sequences.) The problem is that in the original expression, W could be a null pointer dereference guarded by X. In the original code, W would not be executed if X is true, but in the new code, both Z and W will always be executed. The fix is to make the optimizer produce

      Y = (X&&Z)||((!X)&&W)

      If Z and/or W turn out to be safe to execute speculatively, later optimizations in the optimizer can turn the && operators into & operators.

            syncuser TI User
            syncuser TI User
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: