Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp505358lqo; Thu, 16 May 2024 12:28:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUGTsDPWu9dGKB7DO63kn52ReF0aM5YAGTJxupmrDLg70IFTm8Nfu2KBws+ocAF7RTplj++ElSjcxS64lJdwsEBfmj5IDa8pFfacSlYkw== X-Google-Smtp-Source: AGHT+IGNZIyEUz1ZPT+GzCjGklQn7mq0YnsQQIi0v3RnYsmjXoQDXm+HtXtar9Br8N1uerUiTS9E X-Received: by 2002:a17:903:11c7:b0:1ec:3c71:eae7 with SMTP id d9443c01a7336-1ef44160d99mr234771215ad.47.1715887710856; Thu, 16 May 2024 12:28:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715887710; cv=pass; d=google.com; s=arc-20160816; b=d5LaNVnr7KulCB4A337fODgWeW28LS8TLJ6yA+Yb+XEzZf5gCp7R8huR19f8pfijx0 7azifJo1kQKkNM7DGHoYzQugqHY+s0OLXlrXaNuMU9M/RmweB5aRDZvPov6R3/bBgPd0 k7qaD7p1k48eCMfeGsq+s9mfYYRwz0umU4FwtegIFjabNAMXPvGLbabtawipqHZvr0Yz pE4SCekfoDKYjLmEVWVn6RHStigVzwkA1p0ba8PnPLrrLjR0vTAGusoYX9g2K2VGBdMs DOzMncIJTyJDnizmBXyrRNs+TQ+qQo9qYEBvSE3K8GkOMX2UMOTjTQA6YZORVfiDrMLN 3wwQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=IsRVprSkTcedn+zbjClnxOQsSEIZcaVQUIwJQMEacFU=; fh=DGE/Rt7UZyKJ8ICVIgmeDLsrdvw5JULr/fcEfbQDVFQ=; b=U4TPy4Q67VUFZRtsh/aIb6tCz67SlDTp6o8nkMqWr6ydqTuDmWBLleAsoUGlXtsZgo 3m9ru0UUDsaKmAzTA4YUb2pLEryAA7SmCB7J7eDLfTfizbNlwBMnS4/n9FMUIP2BdzBR pVnMxlNmIBG5OLuju10q6ucjIy9pPPVqZK2aqZQn4NVFoHCwQLBLDcnGtC26V6OlvoIB w9UNbuUQNcFP6iy3rAfZwj6NOyy9vKRIaxmoEwGs8BWT9NvEaVSp/zALbMp0QSTmzE5I Dxk/WXmuOUXtBkm5PtsqYiFMhcg+9fYsCJHvklWGup/8tygwq/w44c0KmJjxA4PdQdF4 +FQw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=Lqi5I+J7; arc=pass (i=1 spf=pass spfdomain=cisco.com dkim=pass dkdomain=cisco.com dmarc=pass fromdomain=cisco.com); spf=pass (google.com: domain of linux-kernel+bounces-181517-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181517-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cisco.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id d9443c01a7336-1ef0c039dbesi153685125ad.419.2024.05.16.12.28.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 12:28:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-181517-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=Lqi5I+J7; arc=pass (i=1 spf=pass spfdomain=cisco.com dkim=pass dkdomain=cisco.com dmarc=pass fromdomain=cisco.com); spf=pass (google.com: domain of linux-kernel+bounces-181517-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-181517-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cisco.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id B6F2CB2504D for ; Thu, 16 May 2024 19:12:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 88E72158D7F; Thu, 16 May 2024 19:07:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=cisco.com header.i=@cisco.com header.b="Lqi5I+J7" Received: from aer-iport-8.cisco.com (aer-iport-8.cisco.com [173.38.203.70]) (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 8F7FD15820F; Thu, 16 May 2024 19:07:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.38.203.70 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715886464; cv=none; b=T/WqDmcfb4dDl3kYEzWlLM44M7Q/E4TGJ6uhq7nkLxy0ThEGfI7GLGknN2dkwsCxKhClyiK9qUV/H9nAOiuVbFeAzRRSw75CRiBsgIIWVJRMjfrPxJsdX50zp5PxbrCiFux9lUJ8qypugiSj6QLYkCpRZ27NOL5WacAVmXnMuoQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715886464; c=relaxed/simple; bh=MqQvAYPJnEbPgiMJ/KAsv+WELCKQHh977LECrhqpreU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MEgqz3MAsa8DW0thNqCsuVfyfroRMahZay7GBiLnZCGeaKbU0PpymPOtkLuk3m71Us0HxAA0lC5HWm/Sc17UYS59nG8HtAVHa6OIaXzWEE+WZ+zN72eDmIWVq0nU5O1rgQT02Z0FfUvu2ofKaZR6dAttqrklNwAz7etEXEaTVOY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cisco.com; spf=pass smtp.mailfrom=cisco.com; dkim=pass (1024-bit key) header.d=cisco.com header.i=@cisco.com header.b=Lqi5I+J7; arc=none smtp.client-ip=173.38.203.70 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=cisco.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=cisco.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cisco.com; i=@cisco.com; l=2431; q=dns/txt; s=iport; t=1715886463; x=1717096063; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IsRVprSkTcedn+zbjClnxOQsSEIZcaVQUIwJQMEacFU=; b=Lqi5I+J7p5BD3bJajeuIzqedcy7H3K15Zrb85zx96f4dTmh+tDeoi8qI sJe2nXF1oH5jTqMz3Xji5Jj0S/FmJ5aQJlymHNNVAD/rc4eWCF9Nt7EzD 3/FZq5R/EV2nomvYvdbOMBr/3JM8ubZZtrueXo9enk05zrbaybczSNTNJ Q=; X-CSE-ConnectionGUID: 3vlKbWuhRUSRVklW0FF6rQ== X-CSE-MsgGUID: e4J0BcmTTdyIH+jEv28DOg== X-IronPort-AV: E=Sophos;i="6.08,165,1712620800"; d="scan'208";a="9784124" Received: from aer-iport-nat.cisco.com (HELO aer-core-6.cisco.com) ([173.38.203.22]) by aer-iport-8.cisco.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2024 19:04:24 +0000 Received: from localhost (ams3-vpn-dhcp4879.cisco.com [10.61.83.14]) (authenticated bits=0) by aer-core-6.cisco.com (8.15.2/8.15.2) with ESMTPSA id 44GJ4OOw006188 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 16 May 2024 19:04:24 GMT From: Ariel Miculas To: rust-for-linux@vger.kernel.org Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, tycho@tycho.pizza, brauner@kernel.org, viro@zeniv.linux.org.uk, ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, shallyn@cisco.com, Ariel Miculas Subject: [RFC PATCH v3 19/22] Add borrow_mut implementation to a ForeignOwnable CString Date: Thu, 16 May 2024 22:03:42 +0300 Message-Id: <20240516190345.957477-20-amiculas@cisco.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240516190345.957477-1-amiculas@cisco.com> References: <20240516190345.957477-1-amiculas@cisco.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Authenticated-User: amiculas@cisco.com X-Outbound-SMTP-Client: 10.61.83.14, ams3-vpn-dhcp4879.cisco.com X-Outbound-Node: aer-core-6.cisco.com Since the `borrow_mut` function was added to the ForeignOwnable trait, we need to implement it for each type that implements it. Signed-off-by: Ariel Miculas --- rust/kernel/str.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index c45612900fe2..b0a35d71bd49 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -197,6 +197,25 @@ pub unsafe fn from_char_ptr<'a>(ptr: *const core::ffi::c_char) -> &'a Self { unsafe { Self::from_bytes_with_nul_unchecked(bytes) } } + /// Like from_char_ptr, but returns a mutable reference + /// + /// # Safety + /// + /// `ptr` must be a valid pointer to a `NUL`-terminated C string, and it must + /// last at least `'a`. When `CStr` is alive, the memory pointed by `ptr` + /// must not be mutated. + #[inline] + pub unsafe fn from_char_ptr_mut<'a>(ptr: *const core::ffi::c_char) -> &'a mut Self { + // SAFETY: The safety precondition guarantees `ptr` is a valid pointer + // to a `NUL`-terminated C string. + let len = unsafe { bindings::strlen(ptr) } + 1; + // SAFETY: Lifetime guaranteed by the safety precondition. + let bytes = unsafe { core::slice::from_raw_parts_mut(ptr as _, len as _) }; + // SAFETY: As `len` is returned by `strlen`, `bytes` does not contain interior `NUL`. + // As we have added 1 to `len`, the last byte is known to be `NUL`. + unsafe { Self::from_bytes_with_nul_unchecked_mut(bytes) } + } + /// Creates a [`CStr`] from a `[u8]`. /// /// The provided slice must be `NUL`-terminated, does not contain any @@ -901,6 +920,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { impl ForeignOwnable for CString { type Borrowed<'a> = &'a CStr; + type BorrowedMut<'a> = &'a mut CStr; fn into_foreign(self) -> *const core::ffi::c_void { Box::into_raw(self.buf) as _ @@ -910,6 +930,10 @@ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a> { unsafe { CStr::from_char_ptr(ptr.cast::()) } } + unsafe fn borrow_mut<'a>(ptr: *const core::ffi::c_void) -> Self::BorrowedMut<'a> { + unsafe { CStr::from_char_ptr_mut(ptr.cast::()) } + } + unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { // SAFETY: The safety requirements of this function satisfy those of `Self::borrow`. let str = unsafe { Self::borrow(ptr) }; -- 2.34.1