xv6-public icon indicating copy to clipboard operation
xv6-public copied to clipboard

Improve strlen

Open eeropomell opened this issue 2 years ago • 1 comments

eeropomell avatar Jul 19 '22 20:07 eeropomell

Please consider to:

  1. Fix the line while (s[++x); (missing enclosing square bracket):
  2. 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;
}
  1. Remove .vscode directory from PR

mosolovsa avatar Nov 14 '22 08:11 mosolovsa