From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id E72B03858C98 for ; Mon, 4 Dec 2023 14:44:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E72B03858C98 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E72B03858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701701054; cv=none; b=EkdQsSZOhQTHJtp3t+v93CzN07sQx+zIM9v7F/aZJv1u2jVyhbYLHJvx4Ow6CDsU9XX/2RYSisiM67ds0PjTlnD/3bYALUV01AJIrVrvzBbJ/2Vjtz/ExVACXfPxGHKOrSE9nLOfLfdoYRPNDVXxz9/AR+FMqMzRqUQgRDGJ0QE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701701054; c=relaxed/simple; bh=0f5klihdP14Tcxfth47LzqaHON2OTWIvh3Delg5mKls=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=J1rVHWj8TcA5vEOBLaS/cttv9QqMeb19dJDrsBa5m9wA72EhqNwiIB96Zj7LdxUw091Nvb5AOTxJvnQUoO13FEqfcUNln+wLOBPeU9F194gUHFD8EbIPgLQLdquXi1sZtdO3CqH56n+6LDgi111vxWKmkWOVsadoARGh85RWEEc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701701052; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PdzNgZPZUJDdlzZea8p+/aaHL6R6Q02drMyWKTAIM3c=; b=QN40QOud8cd4KNKGxzxek39Z++7MG4Mhb7TzUFOlZTGzyzUw+PZgraFhk1om22EA1kwP1a /w3TLCkFLiTZysR0ZB53mDcjH0wKtmrTtyRBq7JqJpChH+SOncU7OrfMLxkiLKRphOQXeo UHvwMtbDFdYRFd4XFK+NMFxzAEIso54= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-139-SLmvxSPkN-umkf3LZ1caKA-1; Mon, 04 Dec 2023 09:44:10 -0500 X-MC-Unique: SLmvxSPkN-umkf3LZ1caKA-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-67a940dcd1aso49161966d6.3 for ; Mon, 04 Dec 2023 06:44:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701701050; x=1702305850; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PdzNgZPZUJDdlzZea8p+/aaHL6R6Q02drMyWKTAIM3c=; b=vrboRq0kxw/+QsBHNwBZEXFDaHXWyJ4jdBWFuy8tkfo0A8WrdguTAkiONu/zybBqEr 1ii1OEziyT7WVcp396Bav/jfP29CqkfN4laT3I9kLwQJpvoFe6OAxxnLDVjuV3U/gHUM lEHzDCSuvA176BhiKPsJHdXURYuq/+HkDi23EaSXuMIWa+hAE3QFZNbsWzIX/MQ8wHjr IyrGWTM/PJ+pVEaGWyhs9/dZUbvJi/I1uuL0YFqaaRN+NOIBdkUBELuQ3d2/vpdvvcDh gR1OkP+Z1/gkHbfT0KHQ3nFUISsuBpzDQ4j0eN4aEB/N5w/1vFd/KaJPVcfwMLZoHkGI BKbg== X-Gm-Message-State: AOJu0YwoOhiTdvCeNO14RkMfo5Bi79EkQHmKMkN9Fmr4rE5QslN+VzW0 sFYEWrKvbo7ZtKtwsONFzvYDlKoGGeBOjSqAxcEQsfaug1id+gNYyFu8/T3m20sALZx/nmtcUbX tpLTjIIch/OVbmwzNWSdF X-Received: by 2002:a0c:f646:0:b0:67a:a721:8329 with SMTP id s6-20020a0cf646000000b0067aa7218329mr4584936qvm.131.1701701049919; Mon, 04 Dec 2023 06:44:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IFjW82SOpIXyXoTcrhm1w9Z0MEkyLVfBZe5GaCITlboAJNUJxicYzOKRqCbuPwir5dhm5wDgg== X-Received: by 2002:a0c:f646:0:b0:67a:a721:8329 with SMTP id s6-20020a0cf646000000b0067aa7218329mr4584929qvm.131.1701701049654; Mon, 04 Dec 2023 06:44:09 -0800 (PST) Received: from [192.168.0.241] ([198.48.244.52]) by smtp.gmail.com with ESMTPSA id x14-20020a0ce24e000000b0067a85603a45sm2373213qvl.7.2023.12.04.06.44.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 04 Dec 2023 06:44:09 -0800 (PST) Message-ID: <11f9503d-7b34-4419-3f6e-7a47bd1c7f41@redhat.com> Date: Mon, 4 Dec 2023 09:44:08 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 Subject: Re: [PATCH] strcasestr: try to find non-alpha char in NEEDLE To: James Tirta Halim , wilco.dijkstra@arm.com, Adhemerval Zanella Cc: libc-alpha@sourceware.org References: <20231128140137.81955-1-tirtajames45@gmail.com> From: Carlos O'Donell Organization: Red Hat In-Reply-To: <20231128140137.81955-1-tirtajames45@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 11/28/23 09:01, James Tirta Halim wrote: > --- James, May you please clarify your copyright assignment status? Please review "2.1. Copyright FSF or disclaimer" in the contribution checklist: https://sourceware.org/glibc/wiki/Contribution%20checklist#Copyright_FSF_or_disclaimer > string/strcasestr.c | 37 ++++++++++++++++++++++++++++++------- > 1 file changed, 30 insertions(+), 7 deletions(-) > > diff --git a/string/strcasestr.c b/string/strcasestr.c > index 2f6b4f8641..65eae2f047 100644 > --- a/string/strcasestr.c > +++ b/string/strcasestr.c > @@ -54,7 +54,6 @@ > #define STRCASESTR __strcasestr > #endif > > - > /* Find the first occurrence of NEEDLE in HAYSTACK, using > case-insensitive comparison. This function gives unspecified > results in multibyte locales. */ > @@ -63,18 +62,42 @@ STRCASESTR (const char *haystack, const char *needle) > { > size_t needle_len; /* Length of NEEDLE. */ > size_t haystack_len; /* Known minimum length of HAYSTACK. */ > + const char *h, *n; > > /* Handle empty NEEDLE special case. */ > if (needle[0] == '\0') > return (char *) haystack; > > - /* Ensure HAYSTACK length is at least as long as NEEDLE length. > - Since a match may occur early on in a huge HAYSTACK, use strnlen > - and read ahead a few cachelines for improved performance. */ > - needle_len = strlen (needle); > - haystack_len = __strnlen (haystack, needle_len + 256); > - if (haystack_len < needle_len) > + /* Try to find a non-alphanumeric character in NEEDLE to pass to > + strchr() while checking if HAYSTACK is as long as NEEDLE. */ > + for (h = haystack, n = needle; *h && isalpha (*n); ++h, ++n); > + if (__glibc_unlikely (*h == '\0')) > return NULL; > + if (*n) { > + size_t shift; > + shift = n - needle; > + haystack = strchr (h + shift, *n); > + if (__glibc_unlikely (haystack == NULL)) > + return NULL; > + haystack -= shift; > + /* Check if we have an early match. */ > + for (h = haystack, n = needle; TOLOWER (*h) == TOLOWER (*n) && *h; ++h, ++n); > + if (*n == '\0') > + return (char *)haystack; > + if (__glibc_unlikely (*h == '\0')) > + return NULL; > + if ((size_t) (n - needle) > shift) > + shift = n - needle; > + /* Since a match may occur early on in a huge HAYSTACK, use strnlen > + and read ahead a few cachelines for improved performance. */ > + needle_len = shift + strlen (needle + shift); > + haystack_len = shift + __strnlen (h + shift, 256); > + if (__glibc_unlikely (haystack_len < needle_len)) > + return NULL; > + } else { > + needle_len = n - needle; > + haystack_len = needle_len + __strnlen (haystack + needle_len, 256); > + } > > /* Perform the search. Abstract memory is considered to be an array > of 'unsigned char' values, not an array of 'char' values. See -- Cheers, Carlos.