wkz / phytool

Linux MDIO register access
GNU General Public License v2.0
143 stars 75 forks source link

Trying to understand some code logic #17

Open smooge opened 5 months ago

smooge commented 5 months ago

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?

wkz commented 4 months ago

You're not missing anything. This is just really bad code from my side :grinning:

The whole mv6 program should really be torn out of this tool, since it only ever worked in combination with a patched kernel that was never publicized anywhere. Truth be told, I'm having a hard time caring about this project, since I've moved to use mdio-tools instead.