Single-character search uses a string literal

ID

c.performance.faster_string_find

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Inefficient Call

Language

C / C++

Description

A single-character search passes a string literal (e.g. "x"). The character overload ('x') is faster because it avoids scanning for the terminating null and the extra length computation. Replace the one-character string literal with a character literal.

Rationale

A single-character search passes a string literal (e.g. "x"). The character overload ('x') is faster because it avoids scanning for the terminating null and the extra length computation. Replace the one-character string literal with a character literal.

The following code illustrates the pattern detected by this rule:

void f(std::string s) {
  // FLAGGED: Single-character search uses a string literal
  s.find("A");

  // FLAGGED: Single-character search uses a string literal
  s.rfind("B");

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.