slackhq / tree-sitter-hack

Hack grammar for tree-sitter
MIT License
33 stars 16 forks source link

[BUG] `str` returns a pointer to associated with a local variable (use after free) #37

Closed theHamsta closed 2 years ago

theHamsta commented 2 years ago

Describe the bug

Clang returns a warning about returning a pointer to a stack variable

  CXX(target) Release/obj.target/tree_sitter_hack_binding/bindings/node/binding.o
  CXX(target) Release/obj.target/tree_sitter_hack_binding/src/scanner.o
../src/scanner.cc:106:14: warning: address of stack memory associated with local variable 'str' returned [-Wreturn-stack-address]
      return str.c_str();
             ^~~
1 warning generated.

The affected function return a pointer to a local string that is destroying at the end of function scope. This is a use-after-free bug.

image

Requirements (place an x in each of the [ ])**

To Reproduce

Steps to reproduce the behavior:

Expected behavior

N/A not a runtime bug.

Screenshots

image

Reproducible in:

tree-sitter-hack version: dd7c1bb8b11d290db4aec173740efdb510f0bba9

Run tree-sitter generate with CC=clang-14

OS version(s): Ubuntu 21.10

Additional context

N/A

theHamsta commented 2 years ago

Fixed in https://github.com/slackhq/tree-sitter-hack/pull/38