On Tue, Sep 4, 2012 at 3:40 PM, Martin J. Evans
<
martin...@easysoft.com> wrote:
> I recently received an rt (
https://rt.cpan.org/Ticket/Display.html?id=79190)
> saying some code in DBD::ODBC was wrong and a patch to fix it. The code was:
>
> while (*cp != '\0') {
> *cp++ = toupper(*cp);
> }
>
> and the fix (which shouldn't be required as the above code is fine) was:
>
> while (*cp != '\0') {
> *cp = toupper(*cp);
> cp++;
> }
The order of execution is undefined if you read a variable and assign
to it in the same (sub)statement. The compiler is allowed to read the
*cp on the right both before the cp++, as the only sequence point is
the semicolon at the end.
The patch as offered does the correct and safe thing of splitting it
up in two statement.
> The test code and his results are below. I can obviously avoid this issue
> with a simple change to the code but how many other places might this occur?
> Do I ignore this (as the reporter is happy to use a newer working compiler)
> or do I somehow check for a broken compiler and abort the Makefile? Has
> anyone else come across something like this and what did they do?
You fix it. GCC will actually want you against this if you enable
-Wall/-Wsequence-point
Leon