`DIR-4-15`: Add support for `fpclassify()` functions.

Open
#875 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
devtools

Research direction

Start with the existing DIR-4-15 implementation and the guards library infrastructure referenced in the issue. Compare the three fpclassify guard patterns, including direct conditions, switch cases, and an assigned classification variable. Done means supported guarded conversions no longer produce the error while unguarded potentially unsafe conversions remain diagnosed.

Written by the indexing model from the issue text.

Description

Difficulty-Low false positive/false negative Impact-Low
Affected rules
  • DIR-4-15\
Description

The first implementation of the rule handles guards of the form isinf, isnan, isfinite, iszero, etc., however, it doesn't support guards relating to fpclassify().

Example
float may_be_inf() {
  ...
  return 1.0/0.0;
}

void fpclassify_guard() {
  float x = may_be_inf();
  int y;

  // error, casting possible infinity to int
  y = x; 

  // example 1: easiest to support:
  if (fpclassify(x) != FP_INFINITE) {
    // or "fpclassify(x) == FP_NORMAL"
    y = x;
  }

  // example 2: perhaps slightly harder to support, but guards library already has infra for this:
  switch(x) {
    case FP_NORMAL:
    case FP_SUBNORMAL:
    case FP_ZERO:
      // or, "case FP_INFINITE: break; default:"
      y = x;
  }

  // example 3: this is harder to support but a common pattern:
  int cls = fpclassify(x);
  if (cls != FP_INFINITE) {
    // or "cls == FP_NORMAL || cls == FP_ZERO"
    y = x;
  }   
}
Dominant language
CodeQL
Stars
227
Forks
82
Avg merge
6d 7h
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from github/codeql-coding-standards

All issues in github/codeql-coding-standards

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.