xv6-public
xv6-public copied to clipboard
Improve strlen
Please consider to:
- Fix the line
while (s[++x);
(missing enclosing square bracket): - Think about edge cases, e.g. providing empty string as an argument. Preincrement will set x to 1 skipping the character at index 0, introducing a bug. Test:
#include <stdio.h>
#include <string.h>
typedef size_t uint;
uint
broken_strlen(const char *s)
{
int x = 0;
while (s[++x]);
return x;
}
int main() {
const char *shit_happens = "\0some mem after the c str";
uint len_empty = strlen(shit_happens);
uint broken_len_empty = broken_strlen(shit_happens);
printf("len_empty = %zu, broken_len_empty = %zu!\n", len_empty, broken_len_empty);
return 0;
}
- Remove .vscode directory from PR