Do not use (possibly unterminated!) StringRef::data() but str().

(cherry picked from commit b410db2b494549a09087f33dc04ad9e5a6f925b7)
Code correctness (no dyn scopes).

(cherry picked from commit 8f27ac9ee483e6b199bd50621309bcbd034ba808)