From patchwork Sun Apr 19 12:32:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11497317 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BB64D81 for ; Sun, 19 Apr 2020 12:32:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9D8F421974 for ; Sun, 19 Apr 2020 12:32:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Rclp7PEQ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726050AbgDSMcz (ORCPT ); Sun, 19 Apr 2020 08:32:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726024AbgDSMcy (ORCPT ); Sun, 19 Apr 2020 08:32:54 -0400 Received: from mail-lf1-x12b.google.com (mail-lf1-x12b.google.com [IPv6:2a00:1450:4864:20::12b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C555C061A0F for ; Sun, 19 Apr 2020 05:32:53 -0700 (PDT) Received: by mail-lf1-x12b.google.com with SMTP id m2so5555532lfo.6 for ; Sun, 19 Apr 2020 05:32:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e+N+04sq4fMELZipbi9nzCgI/nPNQSn6R7zicJ3dKUk=; b=Rclp7PEQG/Qgne8dsW/2wdlNWJ6pe45PD/rSi40Kz68aO/GtMxai5/B1mPZzKfYsej vpMQCd04jmi//UqzXJFFHw/07pY6p4YeAgvGZUjcfrCoJGEQGzmKSvOq139xiQuZXm35 qNRDysYS5VDQtCZCPHI2R9EAkk7HY4Txq+qYPhG9M1CHl+pMxlwbT6gNi7/ujB50EdyF H6q3OO1iyvFSbKckgNYxKaOkz4pMfhBX+PyBnPGyUvxBDIMvZ1qVFiXl2WBA/3qyv8qL Y6EAPl4SvZ10WzyXDTHQwhEibvo+WmvcPR8N4bdoqxqmHgsuxl/VzDg7gjsSwPgXWVUl vazg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e+N+04sq4fMELZipbi9nzCgI/nPNQSn6R7zicJ3dKUk=; b=Zz/TT9MWguInAi2ExmdQCDI1ohG81Esy3tRpZcAR1BGTXk77oVmWWcLbDkfyl+ISDH IGCffrNQrAbpKvNz4Du1T6/UP+Eh7mGR7MjArs7BP6JeOVSx8AZIA7nDf2cXd7tt3zJR N6QHQE/85eS+d9LUSp0qHVQJmvZsptrVzTvu8WxcypqZANRANaFR6PBw6TwaMSXCXaFG 19HPPzlizrtwIJWqlNl3GNr5S5TVeYYIVrepTUAIfNH5t6T0t9ncbQz+NuiRXHhwTQIO DWbqmR5nREc5WexU1grals3T9ptKRv5Bs42NMqxhCw5RIKGnx4xmzcMKRYV9tt2X/8H5 mMTQ== X-Gm-Message-State: AGi0PuY7dSd4PGtHw2tFBcE4gUPZ8+hy/fyCXlAJhl7sSfbnRG3j7s3y oEBgbsVN8V81/kdklyONfqMqv5Yr X-Google-Smtp-Source: APiQypJfCPl6m0RUrjMdzuLUj2tKSXWFLm43hg4TZS0/NOUN7pWJEi1R5TSKInjDdzM8NDo6SpydDA== X-Received: by 2002:a19:e04a:: with SMTP id g10mr7365457lfj.164.1587299571508; Sun, 19 Apr 2020 05:32:51 -0700 (PDT) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id 73sm1843547ljj.72.2020.04.19.05.32.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2020 05:32:51 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= Subject: [PATCH 2/4] strbuf: introduce `strbuf_attachstr_len()` Date: Sun, 19 Apr 2020 14:32:28 +0200 Message-Id: X-Mailer: git-send-email 2.26.1 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Most callers of `strbuf_attach()` provide `len + 1` for the `mem` parameter. That's a bit tedious and, as we will see in the next commit, also fairly prone to mistakes. Provide `strbuf_attachstr_len()` for this common case to simplify several callers of `strbuf_attach()` by making this new function simply assume that the size of the allocated buffer is one greater than the length. We know that the string has already been allocated with space for the trailing NUL, meaning it is safe to compute `len + 1`. Disallow NULL-pointers entirely. We could handle `(buf, len) == (NULL, 0)` specially, but none of the callers we convert here seem to worry about such a case. Handling this corner case specially can still be done using the regular `strbuf_attach()`. Another edge case is where someone doesn't have a NUL at `buf[len]`, i.e., they are (hopefully) trying to attach a substring of some larger string. One could argue that they should be using `strbuf_attach()` and that this is BUG-worthy, or that it would be easy enough for us to place a NUL there for robustness and carry on. This commit does the latter, but does not have a strong opinion about it. Signed-off-by: Martin Ågren --- strbuf.h | 19 +++++++++++++++++++ apply.c | 2 +- archive.c | 2 +- blame.c | 2 +- convert.c | 4 ++-- imap-send.c | 2 +- merge-recursive.c | 2 +- pretty.c | 2 +- 8 files changed, 27 insertions(+), 8 deletions(-) diff --git a/strbuf.h b/strbuf.h index 2a462f70cc..7d0aeda434 100644 --- a/strbuf.h +++ b/strbuf.h @@ -121,6 +121,25 @@ char *strbuf_detach(struct strbuf *sb, size_t *sz); */ void strbuf_attach(struct strbuf *sb, void *str, size_t len, size_t mem); +/** + * Attach a string to a buffer. You should specify the string to attach + * and its length. + * + * The amount of allocated memory will be assumed to be one greater than + * its length. The string you pass _must_ be a NUL-terminated string. + * This string _must_ be malloc()ed, and after attaching, the pointer + * cannot be relied upon anymore, nor should it be free()d directly. + * + * Do _not_ use this to truncate the string. That is, the length really + * must be `len` already. To truncate (yet keeping track of the amount + * of allocated memory), use `strbuf_attach()`. + */ +static inline void strbuf_attachstr_len(struct strbuf *sb, + char *str, size_t len) +{ + strbuf_attach(sb, str, len, len + 1); +} + /** * Swap the contents of two string buffers. */ diff --git a/apply.c b/apply.c index 144c19aaca..cab4055ea4 100644 --- a/apply.c +++ b/apply.c @@ -3251,7 +3251,7 @@ static int read_blob_object(struct strbuf *buf, const struct object_id *oid, uns if (!result) return -1; /* XXX read_sha1_file NUL-terminates */ - strbuf_attach(buf, result, sz, sz + 1); + strbuf_attachstr_len(buf, result, sz); } return 0; } diff --git a/archive.c b/archive.c index fb39706120..17b8add930 100644 --- a/archive.c +++ b/archive.c @@ -89,7 +89,7 @@ void *object_file_to_archive(const struct archiver_args *args, struct strbuf buf = STRBUF_INIT; size_t size = 0; - strbuf_attach(&buf, buffer, *sizep, *sizep + 1); + strbuf_attachstr_len(&buf, buffer, *sizep); convert_to_working_tree(args->repo->index, path, buf.buf, buf.len, &buf, &meta); if (commit) format_subst(commit, buf.buf, buf.len, &buf); diff --git a/blame.c b/blame.c index 29770e5c81..12ce104fcb 100644 --- a/blame.c +++ b/blame.c @@ -241,7 +241,7 @@ static struct commit *fake_working_tree_commit(struct repository *r, case S_IFREG: if (opt->flags.allow_textconv && textconv_object(r, read_from, mode, &null_oid, 0, &buf_ptr, &buf_len)) - strbuf_attach(&buf, buf_ptr, buf_len, buf_len + 1); + strbuf_attachstr_len(&buf, buf_ptr, buf_len); else if (strbuf_read_file(&buf, read_from, st.st_size) != st.st_size) die_errno("cannot open or read '%s'", read_from); break; diff --git a/convert.c b/convert.c index 5aa87d45e3..9b3a1218a7 100644 --- a/convert.c +++ b/convert.c @@ -467,7 +467,7 @@ static int encode_to_git(const char *path, const char *src, size_t src_len, free(re_src); } - strbuf_attach(buf, dst, dst_len, dst_len + 1); + strbuf_attachstr_len(buf, dst, dst_len); return 1; } @@ -492,7 +492,7 @@ static int encode_to_worktree(const char *path, const char *src, size_t src_len, return 0; } - strbuf_attach(buf, dst, dst_len, dst_len + 1); + strbuf_attachstr_len(buf, dst, dst_len); return 1; } diff --git a/imap-send.c b/imap-send.c index 6c54d8c29d..37e5b13e51 100644 --- a/imap-send.c +++ b/imap-send.c @@ -1212,7 +1212,7 @@ static void lf_to_crlf(struct strbuf *msg) new_msg[j++] = '\r'; lastc = new_msg[j++] = msg->buf[i]; } - strbuf_attach(msg, new_msg, j, j + 1); + strbuf_attachstr_len(msg, new_msg, j); } /* diff --git a/merge-recursive.c b/merge-recursive.c index d92e2acf1e..ef259e4b74 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -2963,7 +2963,7 @@ static int read_oid_strbuf(struct merge_options *opt, free(buf); return err(opt, _("object %s is not a blob"), oid_to_hex(oid)); } - strbuf_attach(dst, buf, size, size + 1); + strbuf_attachstr_len(dst, buf, size); return 0; } diff --git a/pretty.c b/pretty.c index 28afc701b6..e171830389 100644 --- a/pretty.c +++ b/pretty.c @@ -1687,7 +1687,7 @@ void repo_format_commit_message(struct repository *r, char *out = reencode_string_len(sb->buf, sb->len, output_enc, utf8, &outsz); if (out) - strbuf_attach(sb, out, outsz, outsz + 1); + strbuf_attachstr_len(sb, out, outsz); } free(context.commit_encoding); From patchwork Sun Apr 19 12:32:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11497319 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7C5716CA for ; Sun, 19 Apr 2020 12:32:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 642EE21974 for ; Sun, 19 Apr 2020 12:32:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GCWbM2JO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726055AbgDSMc4 (ORCPT ); Sun, 19 Apr 2020 08:32:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726049AbgDSMcz (ORCPT ); Sun, 19 Apr 2020 08:32:55 -0400 Received: from mail-lj1-x243.google.com (mail-lj1-x243.google.com [IPv6:2a00:1450:4864:20::243]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0C38C061A0C for ; Sun, 19 Apr 2020 05:32:54 -0700 (PDT) Received: by mail-lj1-x243.google.com with SMTP id b2so1401785ljp.4 for ; Sun, 19 Apr 2020 05:32:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ond4fCjYXUD6XOn0ReDXyFHTo902oD1vmYkIA/0cHgk=; b=GCWbM2JO9osznmH7No+47XbfEFlvyKnLbbH70kfMtfUgvXiSWd5guRcsooyqf4WfgM 9MtZmHSowdKE/2vZlEpmcLHKiEwGKbjgCJ8rEi83bT5dfL9HOjQcJr+h9aitb1BQ/fRq 9pCDD3yggPmvxApNPnznizsKpd/amvBnDQ+CIxxLlMCXXQNCO2DaYftfWqnSydnDbuOW /h8thxjbdjZYOpsKWV8Cv0CEOjevoNcxdbUDAhk2yXhFoj0w70SDaPpFmCA/4F1p/I33 fwU0ZncW8wXSTX0jW6GRxK9UzB8L8YnXQufP9yCAYqwXoONmjxe4eavSAVaCPt9Ms3NG Oa4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ond4fCjYXUD6XOn0ReDXyFHTo902oD1vmYkIA/0cHgk=; b=ufc55rLA9BzVDi2Q5W5QuyO1btjFYj+tlpOBOjSHG8IjjKTBiMzuCCdUNRDx1c71e7 hF/jNHHqDnyTBCV/7RX9AI6NAb+9diLggk5cl+wWz/KyuUXzQJhw4FNvC+JHfB/C8cCG drLsIOpvQJ72DOKEQvtflSqHJRhMjfZkRdTBs8+2F+AG9V8jguzLMXSdl6WUa8/sWRnq oYBEVi4W5o09bO36fWaCEAT006nBsdjC/38N0RxtxA5qzdP5vFBbQKOd/h6dNpy6LsyA FjyJBc3mNz6g83brBPk/gLKstFge1VU17J2ZujDPIi6G9x1lnfoXqqImQ25OKgGKVYCJ EKrQ== X-Gm-Message-State: AGi0PubPYA0H2o5Wp7zASDPqaRNPEM1ozowgC24wARmJazCkLmJbeFFD u6l+CXc0wUN6INc0kwF/pjjq0qeQ X-Google-Smtp-Source: APiQypJ0CI8hOQU2tM9oeA0mD/CAFuf6xNSAGovb2s9lXO0+wmFYj9LPMGC1sA9gbYQqgRCPaIw9QA== X-Received: by 2002:a2e:9f13:: with SMTP id u19mr3396006ljk.42.1587299573202; Sun, 19 Apr 2020 05:32:53 -0700 (PDT) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id 73sm1843547ljj.72.2020.04.19.05.32.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2020 05:32:52 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= Subject: [PATCH 3/4] strbuf: introduce `strbuf_attachstr()` Date: Sun, 19 Apr 2020 14:32:29 +0200 Message-Id: X-Mailer: git-send-email 2.26.1 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Similar to the previous commit, introduce `strbuf_attachstr()` where we don't even have to pass in the length of the string that we want to attach. Convert existing callers of `strbuf_attachstr()` that use `strlen()`. Note how only one caller passes in `mem == len + 1` and that the others have been using `strbuf_attach()` in direct contradiction to how it was (incorrectly) documented up until a few commits ago. Now that the documentation has been fixed, you might say these are all fine. But the calling convention of `strbuf_attach()` seems sufficiently hard to get right that it's probably a good idea to introduce this helper. This could help reduce reallocations and memory waste. When we pessimistically pass in `strlen(foo)` for `mem`, the strbuf will have `alloc == len` and will do a reallocation, not just to get one more byte for the NUL (which would have been a no-op), but because we're using `ALLOC_GROW` under the hood, we will ask for 16 more bytes and another 50% on top of that. Signed-off-by: Martin Ågren --- strbuf.h | 11 +++++++++++ path.c | 3 +-- pretty.c | 2 +- refs/files-backend.c | 3 +-- trailer.c | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/strbuf.h b/strbuf.h index 7d0aeda434..32cc15de0c 100644 --- a/strbuf.h +++ b/strbuf.h @@ -140,6 +140,17 @@ static inline void strbuf_attachstr_len(struct strbuf *sb, strbuf_attach(sb, str, len, len + 1); } +/** + * Attach a string to a buffer similar to `strbuf_attachstr_len()`. + * Useful if you do not know the length of the string. + */ +static inline void strbuf_attachstr(struct strbuf *sb, char *str) +{ + size_t len = strlen(str); + + strbuf_attach(sb, str, len, len + 1); +} + /** * Swap the contents of two string buffers. */ diff --git a/path.c b/path.c index 9bd717c307..3cd8fd56b4 100644 --- a/path.c +++ b/path.c @@ -815,8 +815,7 @@ const char *enter_repo(const char *path, int strict) char *newpath = expand_user_path(used_path.buf, 0); if (!newpath) return NULL; - strbuf_attach(&used_path, newpath, strlen(newpath), - strlen(newpath)); + strbuf_attachstr(&used_path, newpath); } for (i = 0; suffix[i]; i++) { struct stat st; diff --git a/pretty.c b/pretty.c index e171830389..5ecdf0cbb2 100644 --- a/pretty.c +++ b/pretty.c @@ -590,7 +590,7 @@ static char *replace_encoding_header(char *buf, const char *encoding) return buf; /* should not happen but be defensive */ len = cp + 1 - (buf + start); - strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1); + strbuf_attachstr(&tmp, buf); if (is_encoding_utf8(encoding)) { /* we have re-coded to UTF-8; drop the header */ strbuf_remove(&tmp, start, len); diff --git a/refs/files-backend.c b/refs/files-backend.c index 561c33ac8a..eb058d85b6 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -1511,10 +1511,9 @@ static int commit_ref(struct ref_lock *lock) * the lockfile to. Hopefully it is empty; try to * delete it. */ - size_t len = strlen(path); struct strbuf sb_path = STRBUF_INIT; - strbuf_attach(&sb_path, path, len, len); + strbuf_attachstr(&sb_path, path); /* * If this fails, commit_lock_file() will also fail diff --git a/trailer.c b/trailer.c index 0c414f2fed..56c4027943 100644 --- a/trailer.c +++ b/trailer.c @@ -1095,7 +1095,7 @@ void trailer_info_get(struct trailer_info *info, const char *str, for (ptr = trailer_lines; *ptr; ptr++) { if (last && isspace((*ptr)->buf[0])) { struct strbuf sb = STRBUF_INIT; - strbuf_attach(&sb, *last, strlen(*last), strlen(*last)); + strbuf_attachstr(&sb, *last); strbuf_addbuf(&sb, *ptr); *last = strbuf_detach(&sb, NULL); continue; From patchwork Sun Apr 19 12:32:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Martin_=C3=85gren?= X-Patchwork-Id: 11497321 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B623A6CA for ; Sun, 19 Apr 2020 12:32:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9E88021974 for ; Sun, 19 Apr 2020 12:32:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WbwK4U05" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726063AbgDSMc6 (ORCPT ); Sun, 19 Apr 2020 08:32:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35944 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726049AbgDSMc4 (ORCPT ); Sun, 19 Apr 2020 08:32:56 -0400 Received: from mail-lf1-x144.google.com (mail-lf1-x144.google.com [IPv6:2a00:1450:4864:20::144]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BC40C061A0C for ; Sun, 19 Apr 2020 05:32:56 -0700 (PDT) Received: by mail-lf1-x144.google.com with SMTP id 131so5555448lfh.11 for ; Sun, 19 Apr 2020 05:32:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TAqG+sVxGy+U9oDNxlIW2JQMI1m4Rts+GOsT6gbOdC4=; b=WbwK4U05Ba3DS+2enHz4tsKFrtV708ulH7uhZpRfaAobd0A5i/1zmrXuE1/HQb+0+o ALBkXET/QEJKbSDDl2HYCUbcjj1W0eQWBTE4HeJYSb3mtlymf0CnWGaEQUchSZ9fvRBf 4uPjlUkDlgvtBFOVUsuqeO2gvf22CKTkMYhIF115q9m0tj4s/B45H/KYbBr3OF3Z2LJf ugzOsAvOBarLWw9YBB9BBYU96Mlw7YHIXEXThXSyWMcJPxN3z2clsFgtf8rs4jISp0ZY lx6eNfPDvpalmCW1bZALZIAwxK3nR4ZHRF08xmUofrGRBXqRoa9D6OnqNDrExMRzlcHG 52VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TAqG+sVxGy+U9oDNxlIW2JQMI1m4Rts+GOsT6gbOdC4=; b=EHgCuIG+WvS0o6vUkYWUSFRA992Fth5Q1avNh7TQQhKX4FZgbOPhI3dC0odSXo3+Vu v3flQKCDITkk5jrIAMHW9LVdUrcLQE/jnI6WCTFr4c9vQoZGgB5jl0BxX0/fm8pJvzJF yJlkKubJRPQkWFJ2tOjuYitFpLmRRf2TgNYT85ZM+DKFdJODEhw/lifp6QLn0D+dkGpV x+RG8Ah4HoxpeX6vEMzt6XG0VC+xldRJUWM7bBeuA2Um0HDQUV+0uUS5bEIrfW5v2fS8 JvXU2kOeDgjR3FMArkBxHktxBitMqEZRBUOWlzTdYjIbELiDhfEfTcZ2tG9yA6m3zZZI b95A== X-Gm-Message-State: AGi0PuZxuib1D3i51Gf3Yax7UBuKjYgOcNrom7VDsyCUAveql3FZWw20 Ed21Uw2c5lRjlBDNz4iCCc78W9sM X-Google-Smtp-Source: APiQypLAaW8WirpahBsBoMzmZChALdnJZ7pW3TULL80kOKxf0Qfi/BHwI7mX0s1eXeXsgS3Gp/Ywag== X-Received: by 2002:ac2:5c45:: with SMTP id s5mr7516248lfp.28.1587299574594; Sun, 19 Apr 2020 05:32:54 -0700 (PDT) Received: from localhost.localdomain (31-211-229-121.customers.ownit.se. [31.211.229.121]) by smtp.gmail.com with ESMTPSA id 73sm1843547ljj.72.2020.04.19.05.32.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Apr 2020 05:32:54 -0700 (PDT) From: =?utf-8?q?Martin_=C3=85gren?= To: git@vger.kernel.org Cc: =?utf-8?b?xJBvw6BuIFRy4bqnbiBDw7RuZyBEYW5o?= Subject: [PATCH 4/4] strbuf_attach: prefer `strbuf_attachstr_len()` Date: Sun, 19 Apr 2020 14:32:30 +0200 Message-Id: <80a7f7570540e847ec986c5f3f8a6f4845866f8b.1587297254.git.martin.agren@gmail.com> X-Mailer: git-send-email 2.26.1 In-Reply-To: References: MIME-Version: 1.0 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org After the last few commits, we don't have many users of `strbuf_attach()`. Convert the sites in builtin/am.c, strbuf.c and mailinfo.c. They pass in the same length twice for `len` and `mem` and will eventually hit `realloc(3)`, which will be a no-op. The string in am.c has been constructed using the strbuf machinery in `read_commit_msg()`. In strbuf.c, we've used `reencode_string_iconv()`. In mailinfo.c, we used `reencode_string_len()`. So in all cases, we really do have an extra byte at the end with a NUL. As explained in the previous commit, it's not just that we avoid calling `realloc()` to make room for a single NUL byte that we already have, we avoid asking it for 16 more bytes and another 50% on top of that. Signed-off-by: Martin Ågren --- builtin/am.c | 2 +- fast-import.c | 2 +- mailinfo.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/builtin/am.c b/builtin/am.c index e3dfd93c25..d777855c98 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -1101,7 +1101,7 @@ static void am_append_signoff(struct am_state *state) { struct strbuf sb = STRBUF_INIT; - strbuf_attach(&sb, state->msg, state->msg_len, state->msg_len); + strbuf_attachstr_len(&sb, state->msg, state->msg_len); append_signoff(&sb, 0, 0); state->msg = strbuf_detach(&sb, &state->msg_len); } diff --git a/fast-import.c b/fast-import.c index 202dda11a6..28fbc4792b 100644 --- a/fast-import.c +++ b/fast-import.c @@ -2946,7 +2946,7 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid) cat_blob_write("\n", 1); if (oe && oe->pack_id == pack_id) { last_blob.offset = oe->idx.offset; - strbuf_attach(&last_blob.data, buf, size, size); + strbuf_attachstr_len(&last_blob.data, buf, size); last_blob.depth = oe->depth; } else free(buf); diff --git a/mailinfo.c b/mailinfo.c index c31991e621..942c363bfd 100644 --- a/mailinfo.c +++ b/mailinfo.c @@ -461,7 +461,7 @@ static int convert_to_utf8(struct mailinfo *mi, return error("cannot convert from %s to %s", charset, mi->metainfo_charset); } - strbuf_attach(line, out, out_len, out_len); + strbuf_attachstr_len(line, out, out_len); return 0; }