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

Compiler incorrectly optimizes unsigned loop counter which wraps around

    XMLWordPrintable

Details

    • Bug
    • Status: Fixed
    • Medium
    • Resolution: Fixed
    • Code Generation Tools
    • CODEGEN-7227
    • Hide
      C6000_8.2.0
      C2000_18.1.0.LTS
      C6000_8.3.0
      MSP430_20.2.0.LTS
      C7000_1.4.0.LTS*
      ARM_18.12.0.LTS
      PRU_2.3.0
      ARP32_1.1.0*
      ARM_20.2.0.LTS
      C2000_20.2.0.LTS
      ARM_18.1.0.LTS
      MSP430_18.1.0.LTS
      PRU_2.4.0*
      MSP430_18.12.0.LTS
      C2000_18.12.0.LTS
      Show
      C6000_8.2.0 C2000_18.1.0.LTS C6000_8.3.0 MSP430_20.2.0.LTS C7000_1.4.0.LTS* ARM_18.12.0.LTS PRU_2.3.0 ARP32_1.1.0* ARM_20.2.0.LTS C2000_20.2.0.LTS ARM_18.1.0.LTS MSP430_18.1.0.LTS PRU_2.4.0* MSP430_18.12.0.LTS C2000_18.12.0.LTS
    • Hide
      ARM_18.12.6.LTS*
      C7000_1.4.0.LTS*
      MSP430_18.1.8.LTS
      C6000_8.3.7*
      ARP32_1.1.0*
      MSP430_20.2.1.LTS
      ARM_20.2.1.LTS
      C2000_18.12.6.LTS*
      PRU_2.4.0*
      ARM_18.1.8.LTS
      MSP430_18.12.6.LTS*
      C2000_18.1.8.LTS
      PRU_2.3.4*
      C6000_8.2.9
      C2000_20.2.1.LTS
      Show
      ARM_18.12.6.LTS* C7000_1.4.0.LTS* MSP430_18.1.8.LTS C6000_8.3.7* ARP32_1.1.0* MSP430_20.2.1.LTS ARM_20.2.1.LTS C2000_18.12.6.LTS* PRU_2.4.0* ARM_18.1.8.LTS MSP430_18.12.6.LTS* C2000_18.1.8.LTS PRU_2.3.4* C6000_8.2.9 C2000_20.2.1.LTS
    • default
    • Avoid writing a do-while loop in which the loop variable wraps around on the first iteration. For instance, use a signed loop variable or a while-loop.
    • Hide
      A do-while loop which uses an unsigned loop variable that wraps around on the first iteration (for example, initialised to (unsigned)-1 and incremented before the first end-test) may confuse the compiler into removing the entire loop. It may interpret the -1 as a large constant that is already past the loop's end value, and fold the loop away.
      Show
      A do-while loop which uses an unsigned loop variable that wraps around on the first iteration (for example, initialised to (unsigned)-1 and incremented before the first end-test) may confuse the compiler into removing the entire loop. It may interpret the -1 as a large constant that is already past the loop's end value, and fold the loop away.

    Description

      The attached file contains an implementation of strnlen.

      size_t strnlen_wtf(const char *s, size_t maxlen)
      {
          size_t n = (size_t)-1;
      
          do n++; while ( (*s++)  &&  (n < maxlen) );
          return n;
      }
      

      Build it ...

      % cl2000 -o2 -s try1.c
      

      Inspect the resulting assembly to see that it is optimized into a function that always returns the value 0.

      Please see the associated forum thread for more context.

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: