folly::io::Cursor: fix UndefinedBehaviorSanitizer: nullptr-with-nonzero-offset
Summary: Per standard pointer arithmetic is only defined when keeping pointers within the bounds of an array object. Applying non-zero offset to nullptr (or making non-nullptr a nullptr by subtracting pointer's integral value from the pointer itself) is undefined behavior. Since https://reviews.llvm.org/D66608 `[InstCombine] icmp eq/ne (gep inbounds P, Idx..), null -> icmp eq/ne P, null) LLVM middle-end uses those guarantees for transformations.` and mis-compilations have been observed: - https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190826/687838.html - https://github.com/google/filament/pull/1566 To prevent help weed out bugs before they lead to future miscompilations a new UBSAN check has been added: `nullptr-with-nonzero-offset` - https://reviews.llvm.org/D67122 [UBSan][clang][compiler-rt] Applying non-zero offset to nullptr is undefined behaviour `folly::io::Cursor` does this type of operations when checking if `crtPos_ + N <= crtEnd_`: when it's empty it becomes `nullptr + N <= nullptr`: const uint8_t* crtBegin_{nullptr}; const uint8_t* crtEnd_{nullptr}; const uint8_t* crtPos_{nullptr}; Switch to `uintptr_t` space where the math is no longer UB. Reviewed By: yfeldblum Differential Revision: D33737556 fbshipit-source-id: 588b91ac1387112a6f183edfda5555ca1b7193d8
Showing
Please register or sign in to comment