Skip to content

Fix undefined behaviour in number serialisation.#36

Open
samhocevar wants to merge 1 commit into
fnuecke:masterfrom
samhocevar-forks:fix-unpersist-read-order
Open

Fix undefined behaviour in number serialisation.#36
samhocevar wants to merge 1 commit into
fnuecke:masterfrom
samhocevar-forks:fix-unpersist-read-order

Conversation

@samhocevar

Copy link
Copy Markdown

When optimising, the compiler is allowed to reorder the read calls
in the following code:

return  (uint16_t)read_uint8_t(info) |
       ((uint16_t)read_uint8_t(info) << 8);

Since read_uint8_t() has side effects, this could read a totally
wrong value and has to be rewritten as such:

uint16_t value = (uint16_t)read_uint8_t(info);
value |= (uint16_t)read_uint8_t(info) << 8;
return value;

This bug has been observed with Visual Studio 2019.

When optimising, the compiler is allowed to reorder the read calls
in the following code:

    return  (uint16_t)read_uint8_t(info) |
           ((uint16_t)read_uint8_t(info) << 8);

Since read_uint8_t() has side effects, this could read a totally
wrong value and has to be rewritten as such:

    uint16_t value = (uint16_t)read_uint8_t(info);
    value |= (uint16_t)read_uint8_t(info) << 8;
    return value;

This bug has been observed with Visual Studio 2019.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant