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

strnlen_s with const string not working with smax > string size + 1 #65

Closed
INgoRah opened this issue May 22, 2019 · 1 comment
Closed
Assignees
Labels
Milestone

Comments

@INgoRah
Copy link

INgoRah commented May 22, 2019

When we use the strnlen_s() function it calls _strnlen_s_chk(str,smax,BOS(str)).
BOS(str) will result in the length of the buffer if the compiler can estimate it:
# define BOS(dest) __builtin_object_size((dest),1)

In _strnlen_s_chk() there is a this check:

	if (unlikely(smax > strbos)) {
		......
		return 0;
	}

If the given buffer length (smax) is bigger than the buffer size calculated by the compiler (strbos) it returns 0 and gives a warning.
The test suite does not include some standard cases to cover this, so it passes.
But a simple test like this would fail (would be nice in tests\test_strnlen_s.c:9

    max_len = 9;
    len = strnlen_s ("testing", max_len);

    if (len != 7) {
        printf("%s %u   len=%u  \n",
                     __FUNCTION__, __LINE__, (unsigned)len);
        errs++;
    }

That means: you need to know the string size to get the string size!

@rurban rurban self-assigned this May 22, 2019
@rurban
Copy link
Owner

rurban commented May 22, 2019

Uh, that's awfully wrong logic indeed. strnlen_s should not fail when the actual length is smaller than the maxlen. smax should only limit scanning on overlong strings, when there's still no 0. I have to check the specs on this. Does not make sense at all like this.

@rurban rurban added the bug label May 22, 2019
rurban added a commit that referenced this issue May 23, 2019
which is obviously an embarrassing bug
rurban added a commit that referenced this issue May 23, 2019
which is obviously an embarrassing bug
rurban added a commit that referenced this issue May 24, 2019
@rurban rurban added this to the 3.5 milestone May 24, 2019
rurban added a commit that referenced this issue May 24, 2019
@rurban rurban closed this as completed in 4150e72 May 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants