Skip to content

getTypeAtLocation on a JSDoc @template type parameter dereferences a nil symbol #144

Open

Description

@soarkit
  • Repository: pingdotgg/ts-rust
  • Target commit: 2b0aaf111f15
  • File: crates/ts_goport/src/checker/checker_p16.rs:695

Summary

A .ts file whose declaration carries @template T / @template {string} K loads fine, the type parameter is reachable with the real ast::get_node_at_position(..., true), and calling Checker::get_type_at_location on it panics with runtime error: invalid memory address or nil pointer dereference.

Affected code

crates/ts_goport/src/checker/checker_p16.rs:

  687              && self.sym(symbol).flags.intersects(SymbolFlags::EXPORT_VALUE)
  688              && self.sym(symbol).export_symbol.is_some()
  689          {
  690              symbol = self.sym(symbol).export_symbol;
  691          }
  692          self.get_merged_symbol(symbol)
  693      }
  694  
  695      // Go: checker/checker.go:14618 getSymbolOfDeclaration
  696      pub fn get_symbol_of_declaration(&mut self, node: Node) -> SymbolId {
  697          let symbol = node.symbol();
  698          if symbol.is_some() {
  699              let late = self.get_late_bound_symbol(symbol);
  700              return self.get_merged_symbol(late);
  701          }
  702          SymbolId::NIL
  703      }
  704  
  705      // Go: checker/checker.go:14628 getSymbolOfNode
  706      // Get the merged symbol for a node. If you know the node is a `Declaration`, it is more type safe to
  707      // use use `getSymbolOfDeclaration` instead.

Expected behavior

Every reachable helper dereference receives a non-null object or returns safely before the dereference.

Actual behavior

A .ts file whose declaration carries @template T / @template {string} K loads fine, the type parameter is reachable with the real ast::get_node_at_position(..., true), and calling Checker::get_type_at_location on it panics with runtime error: invalid memory address or nil pointer dereference. The sink is checker_p26.rs:762's go_nil_dereference() reached through get_declared_type_of_symbol; the JSDoc type parameter has no symbol in the file's symbol table.

How to reproduce

Load a .ts file with a JSDoc @template T on a declaration, resolve the type-parameter node, and call Checker::get_type_at_location; the process aborts with the nil-dereference panic.

Root cause

get_type_at_location takes the is_type_declaration branch for the type-parameter node and calls get_symbol_of_declaration; for a JSDoc-introduced @template parameter the binder records no symbol, so the result is nil and go_nil_dereference traps. The public checker path never guards this case (the callers that do work only use type parameters declared in .ts syntax, where a symbol exists).

Suggested fix

Guard the nil symbol in get_type_at_location/get_declared_type_of_symbol and return the declared type (or anyType) for JSDoc type parameters without a symbol. Add a checker test that loads a .ts file with @template and calls the public API on the type-parameter node.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions