From nobody Fri Dec 19 13:52:03 2025 Received: from mail-10629.protonmail.ch (mail-10629.protonmail.ch [79.135.106.29]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 22D33218845 for ; Thu, 27 Mar 2025 14:50:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=79.135.106.29 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743087020; cv=none; b=kclf8SxTddSrR5JXAtX5uV91ZydHkvoLcooYACx/LYFyavMGKnCts9ErxYxDdodEXn2LeoKbO5XQn7GGrJazmgdrsWuqzgUY+pxvUvgbZvKigJ33u18vFvkYVMY/2ZYsU0vX+ERjrTbbZWwY5BmwYTVABZHwRY86fXy8HYh/xkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743087020; c=relaxed/simple; bh=y+dNdt31+vy08/TWIUqkOmDZKgyr9J8hDr4RBIpTPYQ=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KIS032tytX4IOra8g4LRxA3VXbglmdkoUVvpmFbyon1gCpemBddB9QbbspkTFqqnEOPMep3R8bYXNpgh5oDrwVt83xlzJuYUvQwJCDoaXLCLCHlZiiCgVKGQ8WSe6yKTBrF5OX6cbR/+Vjiy1UWeXkgAcYqRA2vJQ7R0IJ27r7w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=protonmail.com; spf=pass smtp.mailfrom=protonmail.com; dkim=pass (2048-bit key) header.d=protonmail.com header.i=@protonmail.com header.b=EL+IVNhs; arc=none smtp.client-ip=79.135.106.29 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=protonmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=protonmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=protonmail.com header.i=@protonmail.com header.b="EL+IVNhs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1743087010; x=1743346210; bh=PFXXGDMbpQFvQ/hQaIEg3jpMkWzB+T8hkAladjtD21o=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=EL+IVNhsayOu8kVwMlfVd3nq5EwD2JIjBGfsyqtK6puSJja3BJzR5S/vpm4y5e8Pk nmPqI4Ww0vr/fwnedV8w/ki1WyWfR+4iIaLYjSUH77ZIwhAiDLpUWUiTKAvpOx+DU9 X9O76P3S5U+yFvj98pkIJJvx/cYPusv5PrIQz/XuoBm3IrMyEV4aiHZol+VSfw9kpy 2aHAp6eJCxZ3dr2Os2zxayffQbGdW2XNbezZdEEZ/xqP5vdBl0BDstMyk4Ny3KhdNk W4dtRC6iM3xRHHoEKJF0onAwGaUmADcnvv9U+YmT7JOHTMHz9Outruw8S00LMFlse1 WiEUxaf5TnUTQ== Date: Thu, 27 Mar 2025 14:50:05 +0000 To: kent.overstreet@linux.dev, linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org From: Roxana Nicolescu Cc: mmpgouride@gmail.com, skhan@linuxfoundation.org, linux-kernel-mentees@lists.linux.dev Subject: [PATCH v2 1/2] bcachefs: replace strncpy() with memcpy_and_pad in journal_transaction_name Message-ID: <20250327144956.1974622-2-nicolescu.roxana@protonmail.com> In-Reply-To: <20250327144956.1974622-1-nicolescu.roxana@protonmail.com> References: <20250327144956.1974622-1-nicolescu.roxana@protonmail.com> Feedback-ID: 136600343:user:proton X-Pm-Message-ID: f047c589a448da8fe068c37defd649133c6a5899 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Strncpy is now deprecated. The buffer destination is not required to be NULL-terminated, but we also want to zero out the rest of the buffer as it is already done in other places. Link: https://github.com/KSPP/linux/issues/90 Signed-off-by: Roxana Nicolescu --- fs/bcachefs/btree_trans_commit.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/bcachefs/btree_trans_commit.c b/fs/bcachefs/btree_trans_com= mit.c index c4f524b2ca9a..794b70cc98d7 100644 --- a/fs/bcachefs/btree_trans_commit.c +++ b/fs/bcachefs/btree_trans_commit.c @@ -364,7 +364,8 @@ static noinline void journal_transaction_name(struct bt= ree_trans *trans) struct jset_entry_log *l =3D container_of(entry, struct jset_entry_log, entry); =20 - strncpy(l->d, trans->fn, JSET_ENTRY_LOG_U64s * sizeof(u64)); + memcpy_and_pad(l->d, JSET_ENTRY_LOG_U64s * sizeof(u64), + trans->fn, strlen(trans->fn), 0); } =20 static inline int btree_key_can_insert(struct btree_trans *trans, --=20 2.34.1 From nobody Fri Dec 19 13:52:03 2025 Received: from mail-24418.protonmail.ch (mail-24418.protonmail.ch [109.224.244.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 501DA218823 for ; Thu, 27 Mar 2025 14:50:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=109.224.244.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743087016; cv=none; b=MNZ2DDMFGWY3YondPvr82HlQEPjEIfP4fWjRrFp6fjQmm1VGXtlvwZKCKvYJ+2J0Jr3EA2VOSePMMYg7W8pEbriN8bJ2yIY6rzl3UiiYB47InCgokNY2Bfv3wl3ewoYHPtLWvLUvml9RzSo3qHcuOuYJSuN/gGmkaQrlfb/LcSQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743087016; c=relaxed/simple; bh=ixowdw3/tBH1MKs5lErEbKIY7YjR630dlJ2HH1jf3qA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=EkCf9wExebre757eJtpi8WIL5wNhP4NbYGN3aWBhScBYHEx+lfC9ZXEDnif655xcp6Qn73EWpQfKgHO4xFA327wjZ/mc2f8S19FCBl6iT0+Rb4MOSWvgVImplwXO64YAwX7Pq4gFUAm8e2oegaPlVGaGk/qph1/zoKDz2b3VP4w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=protonmail.com; spf=pass smtp.mailfrom=protonmail.com; dkim=pass (2048-bit key) header.d=protonmail.com header.i=@protonmail.com header.b=F7orR4wr; arc=none smtp.client-ip=109.224.244.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=protonmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=protonmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=protonmail.com header.i=@protonmail.com header.b="F7orR4wr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1743087012; x=1743346212; bh=ixowdw3/tBH1MKs5lErEbKIY7YjR630dlJ2HH1jf3qA=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=F7orR4wrE72aQbPzGmqnOiPkwLePLaUAq4hPaWMjLJbCKtYm5dh8xp6Lmoitsj4LV 6JCrlez0LOVkt6Z9jEyXpK0aVInXdigdYSs5sKhDmwJ5kJN9ifeZQMqRWTquwNET+b Lt6ykNIK5x7xQe1jGRjLbOdEK4Ryj9VQarDojJkTGlb51btIh1mBa6UiZv03akXgHx DqdSpKEcp2NIRbsxjsWq7q5X/4duIwh1HX0nGbkAblXOOVGhR9XfFZRGiroX+AFqtG XulK3+1GK1NP0RqWlZU9yBq3iyovituBGDJ0Cs10lJRr/IiIOGnVXfLSdyq0ndZdpH GWPOOp9O1xs/A== Date: Thu, 27 Mar 2025 14:50:09 +0000 To: kent.overstreet@linux.dev, linux-bcachefs@vger.kernel.org, linux-kernel@vger.kernel.org From: Roxana Nicolescu Cc: mmpgouride@gmail.com, skhan@linuxfoundation.org, linux-kernel-mentees@lists.linux.dev Subject: [PATCH v2 2/2] bcachefs: replace memcpy with memcpy_and_pad for jset_entry_log->d buff Message-ID: <20250327144956.1974622-3-nicolescu.roxana@protonmail.com> In-Reply-To: <20250327144956.1974622-1-nicolescu.roxana@protonmail.com> References: <20250327144956.1974622-1-nicolescu.roxana@protonmail.com> Feedback-ID: 136600343:user:proton X-Pm-Message-ID: 47f30f9e3d6a452ed1ff7232a536c31303ac39d8 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This was achieved before by zero-ing out the source buffer and then copying the bytes into the destination buffer. This can also be done with memcpy_and_pad which will zero out only the destination buffer if its size is bigger than the size of the source buffer. This is already used in the same way in journal_transaction_name(). Moreover, zero-ing the source buffer was done twice, first in __bch2_fs_log_msg() and then in bch2_trans_log_msg(). And this method may also require allocating some extra memory for the source buffer. In conclusion, using memcpy_and_pad is better even tough the result is the same because it brings uniformity with what's already used in journal_transaction_name, it avoids code duplication and reallocating extra memory. Signed-off-by: Roxana Nicolescu --- fs/bcachefs/btree_update.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/fs/bcachefs/btree_update.c b/fs/bcachefs/btree_update.c index 13d794f201a5..97495b79ca93 100644 --- a/fs/bcachefs/btree_update.c +++ b/fs/bcachefs/btree_update.c @@ -826,7 +826,6 @@ int bch2_btree_bit_mod_buffered(struct btree_trans *tra= ns, enum btree_id btree, int bch2_trans_log_msg(struct btree_trans *trans, struct printbuf *buf) { unsigned u64s =3D DIV_ROUND_UP(buf->pos, sizeof(u64)); - prt_chars(buf, '\0', u64s * sizeof(u64) - buf->pos); =20 int ret =3D buf->allocation_failure ? -BCH_ERR_ENOMEM_trans_log_msg : 0; if (ret) @@ -839,7 +838,7 @@ int bch2_trans_log_msg(struct btree_trans *trans, struc= t printbuf *buf) =20 struct jset_entry_log *l =3D container_of(e, struct jset_entry_log, entry= ); journal_entry_init(e, BCH_JSET_ENTRY_log, 0, 1, u64s); - memcpy(l->d, buf->buf, buf->pos); + memcpy_and_pad(l->d, u64s * sizeof(u64), buf->buf, buf->pos, 0); return 0; } =20 @@ -852,7 +851,6 @@ __bch2_fs_log_msg(struct bch_fs *c, unsigned commit_fla= gs, const char *fmt, prt_vprintf(&buf, fmt, args); =20 unsigned u64s =3D DIV_ROUND_UP(buf.pos, sizeof(u64)); - prt_chars(&buf, '\0', u64s * sizeof(u64) - buf.pos); =20 int ret =3D buf.allocation_failure ? -BCH_ERR_ENOMEM_trans_log_msg : 0; if (ret) @@ -865,7 +863,7 @@ __bch2_fs_log_msg(struct bch_fs *c, unsigned commit_fla= gs, const char *fmt, =20 struct jset_entry_log *l =3D (void *) &darray_top(c->journal.early_journ= al_entries); journal_entry_init(&l->entry, BCH_JSET_ENTRY_log, 0, 1, u64s); - memcpy(l->d, buf.buf, buf.pos); + memcpy_and_pad(l->d, u64s * sizeof(u64), buf.buf, buf.pos, 0); c->journal.early_journal_entries.nr +=3D jset_u64s(u64s); } else { ret =3D bch2_trans_commit_do(c, NULL, NULL, commit_flags, --=20 2.34.1