Skip to content

Trying to understand some code logic #17

@smooge

Description

@smooge

I started going over the code with cppcheck to learn more about cppcheck, and while I expect it is overly pedantic I came up with some questions

        if (dev < 0xf)
                snprintf(str, sizeof(str), "phy:%d", dev);
        else if (dev == 0xf)
                return "serdes";
        else if (dev)
                snprintf(str, sizeof(str), "port:%d", dev - 0x10);
        else if (dev == 0x1b)
                return "global:1";
        else if (dev == 0x1c)
                return "global:2";
        else if (dev == 0x1d)
                return "global:3";
        else
                snprintf(str, sizeof(str), "port:RESERVED(%#.2x)", dev);

cppcheck complains about the logic in different ways which I am not sure is the right complaints:

print_mv6.c:54:11: style: Condition 'dev' is always true [knownConditionTrueFalse]
 else if (dev)
          ^
print_mv6.c:52:15: note: Assuming that condition 'dev==0xf' is not redundant
 else if (dev == 0xf)
              ^
print_mv6.c:54:11: note: Condition 'dev' is always true
 else if (dev)
          ^
print_mv6.c:56:15: style: Condition 'dev==0x1b' is always false [knownConditionTrueFalse]
 else if (dev == 0x1b)
              ^
... more deleted

Reading through the logic, I read it as if dev is less than 0xf then print A else if dev == 0xf else if dev and the rest of the if would get skipped because the first else says dev must exist and must be greater than 0xf. but this could be me missing another C should know item from being in python-land too long.

If you have time could you explain what I am probably missing in the code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions