Skip to content

Warning: unsequenced modification and access to 'range' #1674

@dmax621

Description

@dmax621
warning: unsequenced modification and access to 'range' [-Wunsequenced]
    if (JSON_LIKELY(*range <= current and current <= *(++range)))
                     ^                                   ~
1 warning generated.
  • Which compiler and operating system are you using? Is it a supported compiler?
    clang, probably version 3.4.0
    CentOS 6 (probably, or possibly 7).
  • Did you use a released version of the library or the version from the develop branch?
    Version 3.2.0
  • If you experience a compilation error: can you compile and run the unit tests?
    Probably not. The warning was reported by our CI system, which runs tests on whichever of many servers is available, and they have different configurations, in particular different compiler & OS versions. I can't tell which specific machine reported this, and I can't reproduce it on my development system (CentOS 7, clang 3.4.2).

The offending line actually looks perfectly OK to me -- and should be a sequence point -- although not for overloaded operator&&, but this instance has a bool on each side (unless there's some bizarre overloading of operator<= going on).

It could be related to clang not quite treating and the same as &&. Or it could be related to the JSON_LIKELY() macro, and misbehaviour of __builtin_expect(x) when x has side-effects.

Anyway, I have fixed this locally by explicitly fetching the range elements in a well-defined sequence (low first, then high) before using them in the comparison (which is then guaranteed to have no side-effects).

@@ class lexer
-        for (auto range = ranges.begin(); range != ranges.end(); ++range)
+        for (auto range = ranges.begin(); range != ranges.end(); )
         {
+            // Fetch range elements in a well-defined order
+            const int low = *range++;
+            const int high = *range++;
 
             get();
-            if (JSON_LIKELY(*range <= current and current <= *(++range)))
+            if (JSON_LIKELY(low <= current and current <= high))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions