Skip to content

Uninitialized variable warning in UARTSerial at -O3 #6611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2018

Conversation

pauluap
Copy link

@pauluap pauluap commented Apr 11, 2018

Description

Appears when complied with -O3 optimization level

Compile: UARTSerial.cpp
../drivers/UARTSerial.cpp: In member function 'void mbed::UARTSerial::tx_irq()':
../drivers/UARTSerial.cpp:314:31: warning: 'data' may be used uninitialized in this function [-Wmaybe-uninitialized]
         SerialBase::_base_putc(data);

Pull request type

[X] Fix
[ ] Refactor
[ ] New target
[ ] Feature
[ ] Breaking change

@pauluap pauluap force-pushed the compiler_warning_UARTSerial branch from 690c8a4 to a75201e Compare April 12, 2018 16:50
@pauluap pauluap changed the base branch from mbed-os-5.8 to master April 12, 2018 16:58
@@ -305,11 +305,11 @@ void UARTSerial::rx_irq(void)
void UARTSerial::tx_irq(void)
{
bool was_full = _txbuf.full();
char data = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this now just masking the problem, which the compiler correctly warned for: if "CricularBuffer::pop()" returns false, the data -variable should not be forwarded to SerialBase::_base_putc()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless there's threading issues involved here, I don't think that pop() will return false because of the check to empty() The compiler is not able to make that rationalization, so I'm not sure if testing the return value of pop() would help.

I suppose that it's writable as

    char data;

    /* Write to the peripheral if there is something to write
     * and if the peripheral is available to write. */
    while (SerialBase::writeable() && _txbuf.pop(data)) {
        SerialBase::_base_putc(data);
    }

Huh, it seems that the compiler was able to analyze the check to pop() after all, with that change I don't get a warning.

I switched the conditional so that it short-circuits the call to pop() if the object isn't writable.

I would go either way, within an IRQ I think that it's safe to call pop after !empty I chose the path of minimal change but could switch.

pop calls empty internally, so the end result is the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this version in the comment without the empty check is better. I think it's only the way it is because it's was imitating the read case.

Appears when complied with -O3 optimization level

Compile: UARTSerial.cpp
../drivers/UARTSerial.cpp: In member function 'void mbed::UARTSerial::tx_irq()':
../drivers/UARTSerial.cpp:314:31: warning: 'data' may be used uninitialized in this function [-Wmaybe-uninitialized]
         SerialBase::_base_putc(data);
@pauluap pauluap force-pushed the compiler_warning_UARTSerial branch from a75201e to d6c5f16 Compare April 16, 2018 14:24
@cmonr
Copy link
Contributor

cmonr commented Apr 17, 2018

Starting CI since it looks like most recent commit addresses @kjbracey-arm' feedback.

/morph build

@mbed-ci
Copy link

mbed-ci commented Apr 17, 2018

Build : SUCCESS

Build number : 1765
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/6611/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

Copy link
Contributor

@kjbracey kjbracey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Pondered performance a bit - this does mean one more serial_writable call per IRQ if we're not saturating the serial port, but does significantly decrease the number of circular buffer calls (which are a bit clunky with IRQ enable/disable). Should be a net performance win when highly loaded, I hope.

@mbed-ci
Copy link

mbed-ci commented Apr 17, 2018

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 17, 2018

/morph export-build

@mbed-ci
Copy link

mbed-ci commented Apr 17, 2018

@mbed-ci
Copy link

mbed-ci commented Apr 17, 2018

@cmonr
Copy link
Contributor

cmonr commented Apr 17, 2018

/morph mbed2-build

@cmonr
Copy link
Contributor

cmonr commented Apr 17, 2018

Attempting the close/reopen trick.

@cmonr cmonr closed this Apr 17, 2018
@cmonr cmonr reopened this Apr 17, 2018
@cmonr cmonr removed the needs: CI label Apr 17, 2018
@mbed-ci
Copy link

mbed-ci commented Apr 17, 2018

Build : SUCCESS

Build number : 1779
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/6611/

Triggering tests

/morph test
/morph uvisor-test
/morph export-build
/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Apr 18, 2018

1 similar comment
@mbed-ci
Copy link

mbed-ci commented Apr 18, 2018

@mbed-ci
Copy link

mbed-ci commented Apr 18, 2018

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 18, 2018

/morph test

@mbed-ci
Copy link

mbed-ci commented Apr 18, 2018

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 19, 2018

/morph export-build

@0xc0170
Copy link
Contributor

0xc0170 commented Apr 19, 2018

/morph mbed2-build

@mbed-ci
Copy link

mbed-ci commented Apr 19, 2018

@cmonr cmonr merged commit f372bcb into ARMmbed:master Apr 23, 2018
@pauluap pauluap deleted the compiler_warning_UARTSerial branch April 23, 2018 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants