antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.12k stars 3.28k forks source link

ATNDeserializationOptions::getDefaultOptions() sometimes returns an uninitialized singleton #1988

Open nliber-ocient opened 7 years ago

nliber-ocient commented 7 years ago

I’m running an app compiled with

g++-7.1 -Og -fsanitize=address -fsanitize=undefined
and when I start to run it the sanitizer is giving me the following error:

include/parser/atn/ATNDeserializationOptions.h:13:26: runtime error: member access within address 0x000020434200 which does not point to an object of type 'ATNDeserializationOptions'
0x000020434200: note: object has invalid vptr
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr
include/parser/atn/ATNDeserializationOptions.h:13:26: runtime error: member access within address 0x000020434200 which does not point to an object of type 'ATNDeserializationOptions'
0x000020434200: note: object has invalid vptr
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr
include/parser/atn/ATNDeserializationOptions.h:13:26: runtime error: member access within address 0x000020434200 which does not point to an object of type 'ATNDeserializationOptions'
0x000020434200: note: object has invalid vptr
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^~~~~~~~~~~~~~~~~~~~~~~
              invalid vptr

I believe the problem is caused by the ATNDeserializer default constructor, which calls ATNDeserializationOptions::getDefaultOptions(), and that call returns the uninitialized singleton ATNDeserializationOptions::defaultOptions.

One way to fix this is to use a Meyers singleton: move the static ATNDeserializationOptions defaultOptions; out of the class ATNDeserializationOptions and into the body of ATNDeserializationOptions::getDefaultOptions().

jasonar81 commented 4 years ago

This is still broken in 4.8. Seems the fix above was not merged?