llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

SmallString/SmallVector don't allow self-assign() or append()

Open brettw opened this issue 3 years ago • 1 comments

The assign(in_iter in_start, in_iter in_end) function of SmallVector (and by extension, SmallString) can't allow assignment to themselves or substrings/subvectors of themselves.

This manifested in a surprising crash in this very innocuous looking code:

SmallString AssetsPath<1024>;
...read AssetsPath...
AssetsPath = llvm::sys::path::parent_path(AssetsPath);  // CRASH

The issue is that parent_path returns a StringRef of a substring of its input. The similar function append(in_iter in_start, in_iter in_end) has the same problem.

I notice that the other assign variant of assign: assign(size_type NumElts, ValueParamT Elt) explicitly handles self-assignment

Related is https://github.com/llvm/llvm-project/issues/25667 which I think is the same underlying problem.

brettw avatar Aug 11 '22 22:08 brettw

Oh, I remember looking into this years ago and kind of running out of steam trying to think about all the cases/how to address them efficiently... so if anyone's interested in this, there's probably some llvm-dev and/or llvm-commits chatter in the archives about the issues here.

dwblaikie avatar Aug 15 '22 20:08 dwblaikie