From: Jim Jagielski Date: Sun, 8 Jan 2012 01:40:01 +0000 (+0000) Subject: optimize this... Do strlen() ONLY if we need to check X-Git-Tag: 2.5.0-alpha~7597 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2232c2c14bbd51967897fc2020e701866d89cbd3;p=thirdparty%2Fapache%2Fhttpd.git optimize this... Do strlen() ONLY if we need to check for overflow. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1228766 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/modules/proxy/proxy_util.c b/modules/proxy/proxy_util.c index 2fa2d7e2f19..5821aa93007 100644 --- a/modules/proxy/proxy_util.c +++ b/modules/proxy/proxy_util.c @@ -81,15 +81,18 @@ APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(proxy, PROXY, int, create_req, PROXY_DECLARE(apr_status_t) ap_proxy_strncpy(char *dst, const char *src, apr_size_t dlen) { - if ((strlen(src)+1) > dlen) { - /* XXX: APR_ENOSPACE would be better */ - return APR_EGENERAL; - } - else { - /* XXX: Once slen and dlen are known, no excuse not to memcpy */ - apr_cpystrn(dst, src, dlen); + char *thenil; + apr_size_t thelen; + + thenil = apr_cpystrn(dst, src, dlen); + thelen = thenil - dst; + /* Assume the typical case is smaller copying into bigger + so we have a fast return */ + if ((thelen < dlen-1) || ((strlen(src)) == thelen)) { + return APR_SUCCESS; } - return APR_SUCCESS; + /* XXX: APR_ENOSPACE would be better */ + return APR_EGENERAL; } /* already called in the knowledge that the characters are hex digits */