Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve strlen #171

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Improve strlen #171

wants to merge 2 commits into from

Conversation

eeropomell
Copy link

No description provided.

@mosolovsa
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants