From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 10728 invoked from network); 31 Dec 2023 05:44:18 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 31 Dec 2023 05:44:18 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1704001458; b=nXBYF0An475pJ9Ox3/BvqmTl+MtCBAIbj5VFKF6NQP4Nw95cwc3/GrglafVwhEQ+zJGQa3eb3p NawIMHXrHpmrs0btdbkOE/JLOXYubnQt8/zhwzju7Lxa/1j3v7+e8ucxsnB9vmD0+EPBxxstzO vcmrP9dFXzb1lMs4C6bLcgHb91M2BYVaqpl4aDrDF7wk1CcYlXChfldh7dfN1o2nQ4dnT5qOGi 6ewBTX7AlAyPU8xy9JrAJo0fV2H1hKutw4x8JnltBZ+fqW38VypvOJl40Odwbt2kLBE7/dsOtr EGFjrEegNyOkYqjmh13t1JUXVqodwBL8vzJ5LHSSaofCBA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-pl1-f175.google.com) smtp.remote-ip=209.85.214.175; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1704001458; bh=gFnSlxkiIkXWjOckKBJUnKm6m3ShvsCsSGtDufTDeB4=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:MIME-Version:Message-ID:Date: Subject:Cc:To:From:DKIM-Signature:DKIM-Signature; b=AanAnXne/ZHa5lzT3X1ncvG+cq1PxfkU5Fdhkpo6MoMl5OQo3GNsLTg5PskpMeDGLthSEDyTYc 8GWgSKhb3nApMEOxqkuCkIqBjUAXXqKYqycC1ttad7kwenihnKhAgB4wHjsAaPYZkDKg2aKOXH E5OdmmMnj6oLe/JyV+9EnWaliitp5fubY7+vD/RUQXkoHJ5t/I4x5VrnRxJ6+LOlUc50Ta82Vn aTYrQOQlhnbMN4oPfLsbELrowvAk8G1pjpiw5LiFbPc6v9l0FI0Lj4KanzKndVgE1+XavghwKF /iJQvYfC9vmB1w7eXS2wta+UsDKs3OyijRps6kNZSlanVA==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: MIME-Version:Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References; bh=yYNEurjR3HpAXexH0bjYKtcceY/KkJA+m3nGWnU4WfY=; b=ZrVn5iGwF4H/O2dW2YycAYsP+K +uNFwVWaFhETnUwkgWzoSbOrl/d2fK8SE0fhUVLF0DeBzrHQ1mG2NpC4fvdZ1nVUE0qZTL3fQFnzz 7hplmhdy7re4X/ylDxj1YcEm6tlLHracsDBF0B9hiYt92DRIWOuP0fTsL09200/q5Ux7mcGdIbS7d ehOa3VWLI+wOAck34rG4300PWjTZ67FnMGLcT+nJ/D0JY1tJePfd2LBRs93SxCOjqVSMOSbEDmB2r RzO6oJKGOkyA09iBLWLtrKfeoka8ipc3idCvaTywKee2VVspaQE9wgjrNumw/9axATzZKbCfD4S+y bm66U41g==; Received: by zero.zsh.org with local id 1rJocU-000Bp8-7l; Sun, 31 Dec 2023 05:44:18 +0000 Authentication-Results: zsh.org; iprev=pass (mail-pl1-f175.google.com) smtp.remote-ip=209.85.214.175; dkim=pass header.d=gmail.com header.s=20230601 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-pl1-f175.google.com ([209.85.214.175]:49480) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1rJocE-000BVu-OA; Sun, 31 Dec 2023 05:44:03 +0000 Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-1d3e8a51e6bso59731705ad.3 for ; Sat, 30 Dec 2023 21:44:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704001440; x=1704606240; darn=zsh.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=yYNEurjR3HpAXexH0bjYKtcceY/KkJA+m3nGWnU4WfY=; b=T5jZ9O5BArk8jA9+NpS+gT+va4OBoMUyROZBtjhYg8acVeRhjm40UOu+ucwkn5jTA1 LV7dCRxT+gaHIV5KNDzenxi/quoje1oj43zg+Fnxo9u/xOj3N6U1S1ogJx15c86zGDgf EvhzeNntu45wHUQyfaRaoJJhS3KbQSrorT10pmTk9SPIkQ1uuetG75Ixo7dEI4U2ItZP RIWVFqQU+XMmlhEIGgPG/yPRDh1rZyMFqtqjeIfnnBDaJdOOYiDSiODFem257rpbpYGo w09squuFFOb4a30Iuq5Rw5geEZTVnhikGm1EgNFuuNe8g+PY5EeZ1cJSHgGKUroAZyXx ckZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704001440; x=1704606240; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yYNEurjR3HpAXexH0bjYKtcceY/KkJA+m3nGWnU4WfY=; b=a9sRYBm/FGLY+lAUrb3LLg22a1lff6981bP7yB56IN6QBblT7vE8ruPX7SsZIDmpS7 mwfv/18pY5DBDflTy9zwPxh3JE81OqUX9WOD+zf+Ad7f7rf4Mnqua5E/8z6PwdpAP7GI zefbny+VvVdA6IiNO85guuDuljgNLJxLkhgumvuK+Q7nY0lhE1PPgFwHNFaU3ZHTE0MM v2HiBiycHwS+rHkE6FQ/s/HdNk/rTQV5A46ciyBZjhtpk8ztYboGAsCAAcEJrfrNMCxR RPnkXMjR9Pewk8rfg4mEumjetSVHJbHc8zMLqL+ZcGAqCrLpepZ9CCvCJ02q7ggTRq+n G/6g== X-Gm-Message-State: AOJu0Yy6QjteCJw/rE+8zqLDA+BfV+gtzvM2Rd0WC2Mao/Hoe66WKMti DztNB2ghCxcQWNCrhDelAFKbfQzzhHtuIA== X-Google-Smtp-Source: AGHT+IEorGX/+92M0g9kWy5Cx2Qh/Z3k/p9SfEwIW7pEeWFbTqqyYggFJwhRkuzBV6TZ77qcr5/78A== X-Received: by 2002:a17:902:f7d5:b0:1d4:208e:a8a0 with SMTP id h21-20020a170902f7d500b001d4208ea8a0mr14260965plw.71.1704001440171; Sat, 30 Dec 2023 21:44:00 -0800 (PST) Received: from localhost.localdomain ([2001:448a:20a0:bcef:5593:1378:bbbb:223f]) by smtp.gmail.com with ESMTPSA id k1-20020a170902ba8100b001d2ed17751asm18076021pls.261.2023.12.30.21.43.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Dec 2023 21:43:59 -0800 (PST) From: James Tirta Halim To: zsh-workers@zsh.org Cc: James Tirta Halim Subject: [PATCH] string.c: prefer memcpy() over strcpy() Date: Sun, 31 Dec 2023 12:43:04 +0700 Message-ID: <20231231054304.48689-1-tirtajames45@gmail.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Seq: 52437 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: Add STRCPY_WLEN, a memcpy() that nul-terminates. --- Src/string.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Src/string.c b/Src/string.c index 5f439926e..fc85da183 100644 --- a/Src/string.c +++ b/Src/string.c @@ -28,6 +28,8 @@ #include "zsh.mdh" +#define STRCPY_WLEN(dst, src, n) (void)(*((char *)memcpy(dst, src, n) + n) = '\0') + /**/ mod_export char * dupstring(const char *s) @@ -36,8 +38,9 @@ dupstring(const char *s) if (!s) return NULL; - t = (char *) zhalloc(strlen((char *)s) + 1); - strcpy(t, s); + const size_t s_len = strlen((char *)s); + t = (char *) zhalloc(s_len + 1); + STRCPY_WLEN(t, s, s_len); return t; } @@ -80,8 +83,9 @@ ztrdup(const char *s) if (!s) return NULL; - t = (char *)zalloc(strlen((char *)s) + 1); - strcpy(t, s); + const size_t s_len = strlen((char *)s); + t = (char *)zalloc(s_len + 1); + STRCPY_WLEN(t, s, s_len); return t; } @@ -148,10 +152,11 @@ dyncat(const char *s1, const char *s2) /* This version always uses space from the current heap. */ char *ptr; size_t l1 = strlen(s1); + size_t l2 = strlen(s2); - ptr = (char *)zhalloc(l1 + strlen(s2) + 1); - strcpy(ptr, s1); - strcpy(ptr + l1, s2); + ptr = (char *)zhalloc(l1 + l2 + 1); + memcpy(ptr, s1, l1); + STRCPY_WLEN(ptr + l1, s2, l2); return ptr; } -- 2.43.0