Skip to content

LGTM.com - false positive in [cpp/constant-comparison] #4249

Closed

Description

@dmiller-nmap

Description of the false positive

Description states "Comparison is always false because n >= 0" but n is returned from Snprintf, which gets it from Vsnprintf(), which gets it from vsnprintf() which may return a negative number in the case of an error (C++ stdio, GlibC, and MSDN docs confirm). Why does LGTM think n must be positive?

URL to the alert on the project page on LGTM.com

https://lgtm.com/projects/g/nmap/nmap/snapshot/e608b14363bf4270b11a15f043b553f1599ea5e1/files/ncat/ncat_listen.c?sort=name&dir=ASC&mode=heatmap#xe60cc1a8fde0e6f6:1

Activity

  1. jbj commented on Sep 11, 2020

    @jbj
    Contributor

    Thanks for the report. I believe the alert is correct even if it's rather confusing.

    When n is a signed int and you check n >= sizeof(buf), you're comparing a signed left-hand side to an unsigned right-hand side of a wider type. In this case, the compiler inserts a cast to make the left-hand side unsigned before comparing. If n is negative, it becomes a very large positive number, and so n >= sizeof(buf) is true and the n < 0 check is skipped.

    It would be safe to remove the n < 0 check, but I definitely understand if you want to keep it for the sake of readers and future maintainers. As a workaround, you can make the alert go away by swapping the order of the operands to ||.

  2. added a commit that references this issue on Sep 14, 2020
  3. added a commit that references this issue on Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions