|
From: | John Reiser |
Subject: | Re: [Qemu-devel] [libfdt][PATCH v2] implement strnlen for systems that need it |
Date: | Sun, 22 Oct 2017 08:05:21 -0700 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 |
... this one is even smaller. Plus it uses the familiar strlen() function: size_t strnlen(const char *string, size_t max_count) { return strlen(string) < max_count ? strlen(string) : max_count; }
Please do not use that implementation. The major goal of strnlen is to avoid looking beyond &string[max_count]. strlen(string) looks all the way to the end, which may be very much longer than max_count; and which may cause SIGSEGV by running into a memory page that does not exist before the terminating '\0' is found. [Besides, some compilers do not recognize that "strlen(string)" need not be evaluated twice.] --
[Prev in Thread] | Current Thread | [Next in Thread] |