3 ms·
int s_isdigit(const char* s) { Name does not fit implementation. Should be "contains_digit". Also I would say almost any "issomething" method involving a loop
by Calzifer 3y ago
int s_isdigit(const char* s) {
Name does not fit implementation. Should be "contains_digit".
Also I would say almost any "issomething" method involving a loop has an opportunity for an early return.
- deleted 3y ago[deleted]
- a1369209993 3y agoAs written, it should be `s_hasdigit`, but it's actually wrong - it should be: int s_isnum(const char* s) { int result = (*s != '\0'); while (*s != '\0') { if ((*s < '0') || ('9' < *s)) { result = 0; } s++; } return result; } otherwise, it will choke on directories like `/proc/etc64/` or `/proc/net6/` if any such directory is added. (Plus other bits like early return, but that's not a correctness bug.)
- adamcc 3y agoThanks for this! Had not been aware of files such as `/proc/net6/` or `/proc/etc64/`. Will include this as an edit to the original post.
- a1369209993 3y agoAFAIK, no such directories exist in the linux proc code at this time. (Which means you won't get bug reports even in principle until, possibly years later, some such directories are added. At which point the code will start to to malfunction, possibly in bizzare and inconsistent ways, despite having worked fine for years by that point. This is a design principle I learned by experience.)