Open llvmbot opened 8 years ago
FWIW our use-before-initialized tool also flagged this case.
Hi Gang Jiang,
Thanks for your report!
The above
ComputeEditDistance
function is used in >"llvm/lib/Support/StringRef.cpp:StringRef::edit_distance()".
"clang/lib/Sema/SemaLookup.cpp" also use llvm::ComputeEditDistance
, but it checks CurNameSpecifierIdentifiers
is NOT empty[1].
Perhaps other components also use it, please use static analyzer, sanitizer, or libfuzzer to double check!
How to fix it?
Other
is empty[2], if empty, early return.m
or n
is zero[3], if zero, early return.[1] https://github.com/llvm-mirror/clang/blob/master/lib/Sema/SemaLookup.cpp#L4342 [2] https://github.com/llvm-mirror/llvm/blob/master/lib/Support/StringRef.cpp#L95 [3] https://github.com/llvm-mirror/llvm/blob/master/include/llvm/ADT/edit_distance.h#L58
Regards, Leslie Zhai
Extended Description
In "llvm/include/llvm/ADT/edit_distance.h:ComputeEditDistance()" function, if two strings(or arrays) are both empty, the result would be a random value, but expected zero. The current version on 2016.07.29 is:
The above algorithm was first introduced on 2015.07.13: git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@242069 91177308-0d34-0410-b5e6-96231b3b80d8. And the last right version is git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240390 91177308-0d34-0410-b5e6-96231b3b80d8.
The above
ComputeEditDistance
function is used in "llvm/lib/Support/StringRef.cpp:StringRef::edit_distance()". So you can reproduce the bug via callingStringRef::edit_distance()
. NOTE that the returned random value may be zero.How to fix it? The simple solution is to initialize the stack-based array explictly:
unsigned SmallBuffer[SmallBufferSize]{};
. The safer solution is to use RAII-style container, however, at the cost for allocating memory dynamicly even if small number of elements.Accordingly, unit test can be strengthened in "llvm/unittests/ADT/StringRefTest.cpp":
That's all.
Gang JIANG jiangg@mail.ustc.edu.cn http://justme0.com/ University of Science and Technology of China