class AdLint::Exam::CBuiltin::W0745

Public Class Methods

new(phase_ctxt) click to toggle source
Calls superclass method AdLint::Examination::new
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 18268
def initialize(phase_ctxt)
  super
  @interp = phase_ctxt[:cc1_interpreter]
  @interp.on_array_subscript_expr_evaled += T(:check_array_subscript)
  @interp.on_indirection_expr_evaled     += T(:check_indirection)
end

Private Instance Methods

check_array_subscript(expr, ary_or_ptr, subs, ary, *) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 18276
def check_array_subscript(expr, ary_or_ptr, subs, ary, *)
  return unless ary

  unless constant_expression?(expr.array_subscript)
    warn_array_oob_access(expr.array_subscript, ary, subs)
  end
end
check_indirection(expr, *) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 18284
def check_indirection(expr, *)
  ary, subs_expr = extract_array_and_subscript(expr.operand)
  return unless ary

  unless constant_expression?(subs_expr)
    subs = interpret(subs_expr, QUIET, WITHOUT_SIDE_EFFECTS)
    warn_array_oob_access(expr.operand, ary, subs)
  end
end
create_subscript_expr(expr, ary_name) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 18335
def create_subscript_expr(expr, ary_name)
  expr.accept(ExpressionTransformer.new(ary_name))
end
extract_array(expr) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 18319
def extract_array(expr)
  collect_object_specifiers(expr).each do |obj_spec|
    if obj = variable_named(obj_spec.identifier.value)
      case
      when obj.type.array?
        return obj, obj_spec.identifier.value
      when obj.type.pointer?
        if obj = pointee_of(obj) and obj.type.array?
          return obj, obj_spec.identifier.value
        end
      end
    end
  end
  nil
end
extract_array_and_subscript(expr) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 18313
def extract_array_and_subscript(expr)
  ary, ary_name = extract_array(expr)
  subs_expr = create_subscript_expr(expr, ary_name)
  return ary, subs_expr
end
interpreter() click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 18339
def interpreter
  @interp
end
warn_array_oob_access(expr, ary, subs) click to toggle source
# File lib/adlint/exam/c_builtin/cc1_check.rb, line 18294
def warn_array_oob_access(expr, ary, subs)
  if ary_len = ary.type.length
    lower_bound = scalar_value_of(0)
    test = subs.value.test_must_be_less_than(lower_bound)
    if test.true?
      W(expr.location,
        *test.evidence.emit_context_messages(self, expr.location))
      return
    end

    upper_bound = scalar_value_of(ary_len - 1)
    test = subs.value.test_must_be_greater_than(upper_bound)
    if test.true?
      W(expr.location,
        *test.evidence.emit_context_messages(self, expr.location))
    end
  end
end