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.133.124]) by sourceware.org (Postfix) with ESMTPS id 720C53858D33 for ; Thu, 20 Apr 2023 08:07:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 720C53858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1681978030; 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: in-reply-to:in-reply-to:references:references; bh=k1O0S8e0QJCGg5VK5YDg/vnseX5M+A+jc3yl4Mr3s0U=; b=ivFrW4PPnCJ+aEhcl7CkjZPN5rtngWbHed5jjgkrZoAwop9qckXs2EIiTW0RtJziSZo5N5 rmNz9yO48ueCWWLovUXgu5Dtj/qiAaF6Tuc3vg8VRQrHg3nVe3thq3JLtQb5cEk89WQHxB jUwy1QHpXzDVHgqMdO1WZT4Ou4tcoTU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-404-N7IUF6BcN2qPFvaW4Om9HA-1; Thu, 20 Apr 2023 04:07:09 -0400 X-MC-Unique: N7IUF6BcN2qPFvaW4Om9HA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B69873802AC8; Thu, 20 Apr 2023 08:07:08 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 103052026D16; Thu, 20 Apr 2023 08:07:07 +0000 (UTC) From: Florian Weimer To: Paul Eggert Cc: libc-alpha@sourceware.org Subject: Re: [PATCH 1/2] Implement strlcpy and strlcat [BZ #178] References: <3e699937-2b0d-7218-3f97-ab54154806c1@cs.ucla.edu> Date: Thu, 20 Apr 2023 10:07:06 +0200 In-Reply-To: <3e699937-2b0d-7218-3f97-ab54154806c1@cs.ucla.edu> (Paul Eggert's message of "Sat, 8 Apr 2023 15:08:34 -0700") Message-ID: <87bkjjgeol.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-4.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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: * Paul Eggert: >> +extern __typeof (strlcpy) __strlcpy; >> +libc_hidden_proto (__strlcpy) >> +extern __typeof (strlcat) __strlcat; >> +libc_hidden_proto (__strlcat) > > Glibc shouldn't call these functions internally, so let's not export > them to elsewhere in glibc. strlcpy looks like it could be called for implementing %s in snprintf. That seems like a reasonable optimization. We would even use the returned length in case the string does not fit. Less sure about strlcat, we could drop the PLT avoidance for that, I assume. >> + /* Copy the leading portion of the string. The last >> + character is subsequently overwritten with the NUL >> + terminator, but the destination size is usually a >> + multiple of a small power of two, so writing it twice >> + should be more efficient than copying an odd number of >> + bytes. */ >> + memcpy (dest, src, size); >> + dest[size - 1] = '\0'; > > This micro-optimization is incorrect, as it's valid for dest to equal I think we concluded that this optimization is in fact correct, right? > src + size - 1, and that means the memcpy overlaps which is > undefined. Change it to memcpy (dest, src, size - 1) and lose the > comment. Or change it to memmove and lengthen the comment. Or better > yet, get rid of all code like this (there are other instances), and > use the simple OpenBSD implementation which will more likely match > what callers expect (in the rare cases where the behaviors differ) and > will possibly be faster despite not using memcpy. I expect someone to rewrite this using word-size accesses fairly soon. I think using strlen and memcpy more clearly documents the intent than the explicit loops. Thanks, Florian