Skip to content

Conversation

bungeman
Copy link
Contributor

@bungeman bungeman commented Apr 7, 2020

In C it is undefined to add anything to NULL. Clang recently began
taking advantage of this and can assume that if anything is added or
subtracted from a pointer that the pointer can be assumed non-NULL. The
Address Sanitizer has been updated to report when this happens at
runtime and produces messages like

expat/lib/xmlparse.c:6509:23: runtime error: applying zero offset to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior expat/lib/xmlparse.c:6509:23

This can be mitigated with 'p ? p + n : NULL' which optimizes to just
the add in all optimizing compilers, but avoids the undefined behavior.

In C it is undefined to add anything to NULL. Clang recently began
taking advantage of this and can assume that if anything is added or
subtracted from a pointer that the pointer can be assumed non-NULL. The
Address Sanitizer has been updated to report when this happens at
runtime and produces messages like

expat/lib/xmlparse.c:6509:23: runtime error: applying zero offset to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior expat/lib/xmlparse.c:6509:23

This can be mitigated with 'p ? p + n : NULL' which optimizes to just
the add in all optimizing compilers, but avoids the undefined behavior.
Copy link
Member

@hartwork hartwork left a comment

Choose a reason for hiding this comment

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

Hi @bungeman , thanks for raising awareness about this issue. I'll need to understand the issue better to be able to evaluate if this is a good approach to a fix or not. Have you traced back where NULL iter->p can be resulting from and what the consequences of NULL iter->end will be, already?

@bungeman
Copy link
Contributor Author

bungeman commented Apr 7, 2020

I honestly haven't looked too hard, but this currently seems to happen any time initializing an iterator on a hashTable which hasn't had anything added to it.

Note that FreeType had a number of similar issues and ended up adding a FT_OFFSET macro for this, it's probably best to clone freetype from git://git.sv.nongnu.org/freetype/freetype2.git and do 'git log -SFT_OFFSET' for the recent history and how it is used there. It may be worth it to do that here, both for future reference and documentation.

hartwork added a commit that referenced this pull request Apr 20, 2020
hartwork added a commit that referenced this pull request Apr 20, 2020
@hartwork hartwork merged commit 49c165c into libexpat:master Apr 20, 2020
@hartwork
Copy link
Member

I have had a closer look at the related code and your suggested fix now. I agree there is a problem and also agree that this is the right move. Thanks for your contribution to libexpat! 👍

@hartwork hartwork self-assigned this Apr 20, 2020
@bungeman bungeman deleted the ubsan branch April 20, 2020 14:59
dand-oss pushed a commit to dand-oss/libexpat that referenced this pull request Apr 28, 2020
@hartwork hartwork added this to the 2.2.10 milestone Oct 2, 2020
tdf-gerrit pushed a commit to LibreOffice/core that referenced this pull request Jul 26, 2021
drop ubsan patch in favour of fix applied as
libexpat/libexpat#398

Change-Id: I59eb9e24206b9a4cf323b7f7d48d8df0792a1c46
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116102
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
(cherry picked from commit 740d12d)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119422
Tested-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
Reviewed-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
tdf-gerrit pushed a commit to LibreOffice/core that referenced this pull request Jul 26, 2021
drop ubsan patch in favour of fix applied as
libexpat/libexpat#398

Change-Id: I59eb9e24206b9a4cf323b7f7d48d8df0792a1c46
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116102
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
(cherry picked from commit 740d12d)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119426
Tested-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
Reviewed-by: Thorsten Behrens <thorsten.behrens@allotropia.de>
tdf-gerrit pushed a commit to LibreOffice/core that referenced this pull request Jul 26, 2021
drop ubsan patch in favour of fix applied as
libexpat/libexpat#398

Change-Id: I59eb9e24206b9a4cf323b7f7d48d8df0792a1c46
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116102
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
(cherry picked from commit 740d12d)
tdf-gerrit pushed a commit to LibreOffice/core that referenced this pull request Jul 26, 2021
drop ubsan patch in favour of fix applied as
libexpat/libexpat#398

Change-Id: I59eb9e24206b9a4cf323b7f7d48d8df0792a1c46
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116102
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
(cherry picked from commit 740d12d)
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.

2 participants