Class: TaintedLove::StackTrace

Inherits:
Object
  • Object
show all
Defined in:
lib/tainted_love/stack_trace.rb

Constant Summary collapse

BACKTRACE_LINE_REGEX =
/^((?:[a-zA-Z]:)?[^:]+):(\d+)(?::in `([^']+)')?$/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack_trace) ⇒ StackTrace

Returns a new instance of StackTrace.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tainted_love/stack_trace.rb', line 9

def initialize(stack_trace)
  @stack_trace = stack_trace
  @lines = stack_trace.map do |line|
    next unless line.match(BACKTRACE_LINE_REGEX)
    {
      file: Regexp.last_match(1),
      line_number: Regexp.last_match(2).to_i,
      method: Regexp.last_match(3),
    }
  end.compact

  # Hack to remove TaintedLove.proxy_method
  @lines.shift if @lines.first[:file]['tainted_love/utils.rb']
end

Instance Attribute Details

#linesObject

Returns the value of attribute lines.



7
8
9
# File 'lib/tainted_love/stack_trace.rb', line 7

def lines
  @lines
end

#stack_traceObject

Returns the value of attribute stack_trace.



7
8
9
# File 'lib/tainted_love/stack_trace.rb', line 7

def stack_trace
  @stack_trace
end

Class Method Details

.current(skip = 2) ⇒ TaintedLove::StackTrace

Create a new StackTrace object from the current thread backtrace

Parameters:

  • skip (Integer) (defaults to: 2)

    number of trace line to skip

Returns:



42
43
44
# File 'lib/tainted_love/stack_trace.rb', line 42

def self.current(skip = 2)
  new(Thread.current.backtrace(skip))
end

Instance Method Details

#to_jsonObject



32
33
34
35
36
# File 'lib/tainted_love/stack_trace.rb', line 32

def to_json
  {
    trace_hash: trace_hash,
  }.to_json
end

#trace_hashString

Produces a hash from the stack trace to identify identical code path

Returns:

  • (String)


27
28
29
30
# File 'lib/tainted_love/stack_trace.rb', line 27

def trace_hash
  lines = @lines.map { |line| "#{line[:file]}:#{line[:number]}" }.join(',')
  TaintedLove.hash(lines)
end