Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp4334058rdb; Mon, 11 Dec 2023 16:43:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZ4MiDe8WHDIgjv6u6S4PM1ptgfnsC+H47JBYzoZFi1wVVESYAqil61BxyUi45mYAiO9su X-Received: by 2002:a05:6a20:748f:b0:18f:97c:8a29 with SMTP id p15-20020a056a20748f00b0018f097c8a29mr7422282pzd.84.1702341788546; Mon, 11 Dec 2023 16:43:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702341788; cv=none; d=google.com; s=arc-20160816; b=GPBu6K+3xUotXErHC7hw2svLfXqdIhmjf4QBAPk7AM66u1yJR4nXzBgimVNcl/4V89 BJZBw3rBap6tSx/BSQRgrcEupYlLSgASpLqEFQg6a5qo3pMbf9+FPgr+v5lYBOw1dSYA 5IfJCPfFKBZY2zBCcECw4AjOZKkDmL4P7J+7c32K6MdtExMbO4jATOc+XMOaAqCO5RSh vGslTQJQmnaBLiPxLe+ZZByd3xylscGajwCSpi1oW0SotW3UcVnn1rLJMwYDpQeikv70 EvqGCy72N30OyZtQy9XyEKoHsDaXTX1cyBic37+p+GdmY+6DVzZLEGb8h+W42rjRP2hW NiFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:mime-version:date :dkim-signature; bh=Wieanvmnd3ObNfpHSltL/XlVJFDJjSL12LZUtqBpsps=; fh=Z5SejTUf02Bhvj9DKrFIdpE5vGgBwCNszVT7KTzIiq8=; b=BRXxS8JR2N9v9rozWuZN4VUVD32fNQiQft4uq5V/ryaHmi3OD4Hou7cgG2ySCSGI7v vUNhVJGsBRhvSoBWi+QWKEnGggo6bjq1oNniYnZVq5B0f9JN3ttwgEcU565x6KXbG83c OJU87EgRRNf7SD7t+yjscdbRLcfQR632jnUtehSEUW3waKalsiMtpUUIHSaBlc+jHScz jI8zCBKxr/SRn5s3cOypEOTwVDNgg891ZhmnrsawFbrXk+I0Xxy+EzDEBxwI3dtxXCX3 J1FMgobwPsH6n+rpvSY3hKV1jiraiuTlZcbNVSEs/ayFSCMfL0m81OGlngspksyGUN8U cI6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=inrg7juA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id b2-20020a63eb42000000b005be209ac7fesi6735259pgk.744.2023.12.11.16.43.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Dec 2023 16:43:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=inrg7juA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id A1BDB80BA43E; Mon, 11 Dec 2023 16:43:05 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345483AbjLLAmu (ORCPT + 99 others); Mon, 11 Dec 2023 19:42:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46344 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345433AbjLLAms (ORCPT ); Mon, 11 Dec 2023 19:42:48 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3094DA6 for ; Mon, 11 Dec 2023 16:42:55 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-db512266d27so5361190276.3 for ; Mon, 11 Dec 2023 16:42:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702341774; x=1702946574; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Wieanvmnd3ObNfpHSltL/XlVJFDJjSL12LZUtqBpsps=; b=inrg7juATPiXPa5xxemaIB1+gm5sMp+ehcSByHAzXt3i5GxRXVSqopvgCrgRJ5h8Yn kLxkD1SQAaJmr9AZZV5PhNkuBq30dnEiOmCfo1/HzkTzHYTXk/Rj9PEueZxetmwtCooa OnPMlLWZZS7FpuWkr5hG0uHTDTsFg5A5fcix8WltSyke4SJSy/gR7dFfQnw3WBc+OAFf UXLlPCQc4jOEhOz4qPjcq8paGsmjSoaNN4WW6BvPA5hRcba2t41U60souoBd0wboWSa/ 4aCiR4itY2qnqo+MJ8mEJyOE5rvyY1VVXb4bpb7hcUa3hSWsZnqYtb398enVx7dC2ybM T4Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702341774; x=1702946574; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Wieanvmnd3ObNfpHSltL/XlVJFDJjSL12LZUtqBpsps=; b=AveuMbxTBNmCEWopwiq181lwzRd3xZlD71tnkbbM/iZhLgXIwV55SB8Mi//BZ/CIyT GbdWjnYOBSp+5UI02gF+Q3R8G8Mqna+V6ZqBFEq9edZBh0eJJqoPC6w3Kcsd/WNwXnma fULovJPYn+L7NxIeJXmmqYDTG4IfJHlzhRxYb5AafQi1DbaiYNxLtBANAfZiXdRqLjUw 8hff9Q/Un95vb8YsIHANO+0cYgDfT2uSGLYtkKZ9gGh7H+OnsF+7XDbCGH2zfCg3PiGx T57ZUbNUdPjifbab5ZHwFYsmb8SQzL+ZSEr0d3iAeUQajbbXZ6Llrm2nSOi5j1VEE6C6 cwgg== X-Gm-Message-State: AOJu0Yxawz6Cj9XUiaGkesif+FxwUi1oWjP8icWmU3soZWKwFn90znKh oIT5MuIcWc5fMzer8L2lViIFW7l5hyBHDdIH4A== X-Received: from jstitt-linux1.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:23b5]) (user=justinstitt job=sendgmr) by 2002:a25:5088:0:b0:dbc:b125:2949 with SMTP id e130-20020a255088000000b00dbcb1252949mr13392ybb.0.1702341774309; Mon, 11 Dec 2023 16:42:54 -0800 (PST) Date: Tue, 12 Dec 2023 00:42:52 +0000 Mime-Version: 1.0 X-B4-Tracking: v=1; b=H4sIAIusd2UC/5XNvQ6CMBSG4VsxnT2mP9iCk/dhHLQ9wEmEkpY0E MK9WxhM3HT8vuF5FxYxEEZ2OSwsYKJIvs9DHQ/Mto++QSCXN5NcKl5JAXEMvR1mcIEShghEHob gJ+ponCFOlZIFWCitKpTRRnNVsYwNAWua9tDtnndLcfRh3rtJbO/fiSRAQIGlk2XtBBp7bbxvX niyvmNbI8mPK7jUP7syuxZR49k9Deff7rqub8IFges1AQAA X-Developer-Key: i=justinstitt@google.com; a=ed25519; pk=tC3hNkJQTpNX/gLKxTNQKDmiQl6QjBNCGKJINqAdJsE= X-Developer-Signature: v=1; a=ed25519-sha256; t=1702341773; l=5459; i=justinstitt@google.com; s=20230717; h=from:subject:message-id; bh=KhNNLMz44VJH+SMkWMDPM/eP4tCUdfa9dI0bKvoxV1k=; b=zpVS2BABeC8dYIUeeu3AhJj62yGiXiCxGsX2m2w6vLIFwGE7jfG0UQ7Vdl3iK99mGrFj1ecjZ elt2HgweUHrD/jm5iajOtueL4EGyLuVXOgKLtwWl9AY1e/9TuMZOnVe X-Mailer: b4 0.12.3 Message-ID: <20231212-strncpy-drivers-iio-proximity-sx9324-c-v3-1-b8ae12fc8a5d@google.com> Subject: [PATCH v3] iio: sx9324: avoid copying property strings From: Justin Stitt To: Jonathan Cameron , Lars-Peter Clausen Cc: Stephen Boyd , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, Justin Stitt Content-Type: text/plain; charset="utf-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 11 Dec 2023 16:43:05 -0800 (PST) We're doing some needless string copies when trying to assign the proper `prop` string. We can make `prop` a const char* and simply assign to string literals. For the case where a format string is used, let's extract the parsing logic out into sx9324_parse_phase_prop(). We no longer need to create copies or allocate new memory. sx9324_parse_phase_prop() will simply return the default def value if it fails. This also cleans up some deprecated strncpy() uses [1]. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: https://github.com/KSPP/linux/issues/90 Cc: linux-hardening@vger.kernel.org Signed-off-by: Justin Stitt --- Changes in v3: - extract logic into sx9324_parse_phase_prop() and use string literals (thanks Stephen) - rebase onto mainline bee0e7762ad2c602 - Link to v2: https://lore.kernel.org/r/20231026-strncpy-drivers-iio-proximity-sx9324-c-v2-1-cee6e5db700c@google.com Changes in v2: - make prop a const char* and do simple assignments (thanks Jonathan) - rebase onto 3a568e3a961ba330 - Link to v1: https://lore.kernel.org/r/20230921-strncpy-drivers-iio-proximity-sx9324-c-v1-1-4e8d28fd1e7c@google.com --- --- drivers/iio/proximity/sx9324.c | 69 ++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/drivers/iio/proximity/sx9324.c b/drivers/iio/proximity/sx9324.c index 438f9c9aba6e..e3bc30b57b19 100644 --- a/drivers/iio/proximity/sx9324.c +++ b/drivers/iio/proximity/sx9324.c @@ -873,6 +873,32 @@ static int sx9324_init_compensation(struct iio_dev *indio_dev) 20000, 2000000); } +static u32 sx9324_parse_phase_prop(struct device *dev, + struct sx_common_reg_default *reg_def, + int idx, const char *prop) +{ + unsigned int pin_defs[SX9324_NUM_PINS]; + int count, ret, pin; + u8 default_def; + u32 raw = 0; + + default_def = sx9324_default_regs[idx].def; + + count = device_property_count_u32(dev, prop); + if (count != ARRAY_SIZE(pin_defs)) + return default_def; + ret = device_property_read_u32_array(dev, prop, pin_defs, + ARRAY_SIZE(pin_defs)); + if (ret) + return default_def; + + for (pin = 0; pin < SX9324_NUM_PINS; pin++) + raw |= (pin_defs[pin] << (2 * pin)) & + SX9324_REG_AFE_PH0_PIN_MASK(pin); + + return raw; +} + static const struct sx_common_reg_default * sx9324_get_default_reg(struct device *dev, int idx, struct sx_common_reg_default *reg_def) @@ -884,35 +910,30 @@ sx9324_get_default_reg(struct device *dev, int idx, #define SX9324_PIN_DEF "semtech,ph0-pin" #define SX9324_RESOLUTION_DEF "semtech,ph01-resolution" #define SX9324_PROXRAW_DEF "semtech,ph01-proxraw-strength" - unsigned int pin_defs[SX9324_NUM_PINS]; - char prop[] = SX9324_PROXRAW_DEF; + const char *prop = SX9324_PROXRAW_DEF; u32 start = 0, raw = 0, pos = 0; - int ret, count, ph, pin; const char *res; + int ret; memcpy(reg_def, &sx9324_default_regs[idx], sizeof(*reg_def)); sx_common_get_raw_register_config(dev, reg_def); switch (reg_def->reg) { case SX9324_REG_AFE_PH0: + reg_def->def = sx9324_parse_phase_prop(dev, reg_def, idx, + "semtech,ph0-pin"); + break; case SX9324_REG_AFE_PH1: + reg_def->def = sx9324_parse_phase_prop(dev, reg_def, idx, + "semtech,ph1-pin"); + break; case SX9324_REG_AFE_PH2: + reg_def->def = sx9324_parse_phase_prop(dev, reg_def, idx, + "semtech,ph2-pin"); + break; case SX9324_REG_AFE_PH3: - ph = reg_def->reg - SX9324_REG_AFE_PH0; - snprintf(prop, ARRAY_SIZE(prop), "semtech,ph%d-pin", ph); - - count = device_property_count_u32(dev, prop); - if (count != ARRAY_SIZE(pin_defs)) - break; - ret = device_property_read_u32_array(dev, prop, pin_defs, - ARRAY_SIZE(pin_defs)); - if (ret) - break; - - for (pin = 0; pin < SX9324_NUM_PINS; pin++) - raw |= (pin_defs[pin] << (2 * pin)) & - SX9324_REG_AFE_PH0_PIN_MASK(pin); - reg_def->def = raw; + reg_def->def = sx9324_parse_phase_prop(dev, reg_def, idx, + "semtech,ph3-pin"); break; case SX9324_REG_AFE_CTRL0: ret = device_property_read_string(dev, @@ -937,11 +958,9 @@ sx9324_get_default_reg(struct device *dev, int idx, case SX9324_REG_AFE_CTRL4: case SX9324_REG_AFE_CTRL7: if (reg_def->reg == SX9324_REG_AFE_CTRL4) - strncpy(prop, "semtech,ph01-resolution", - ARRAY_SIZE(prop)); + prop = "semtech,ph01-resolution"; else - strncpy(prop, "semtech,ph23-resolution", - ARRAY_SIZE(prop)); + prop = "semtech,ph23-resolution"; ret = device_property_read_u32(dev, prop, &raw); if (ret) @@ -1012,11 +1031,9 @@ sx9324_get_default_reg(struct device *dev, int idx, case SX9324_REG_PROX_CTRL0: case SX9324_REG_PROX_CTRL1: if (reg_def->reg == SX9324_REG_PROX_CTRL0) - strncpy(prop, "semtech,ph01-proxraw-strength", - ARRAY_SIZE(prop)); + prop = "semtech,ph01-proxraw-strength"; else - strncpy(prop, "semtech,ph23-proxraw-strength", - ARRAY_SIZE(prop)); + prop = "semtech,ph23-proxraw-strength"; ret = device_property_read_u32(dev, prop, &raw); if (ret) break; --- base-commit: bee0e7762ad2c6025b9f5245c040fcc36ef2bde8 change-id: 20230921-strncpy-drivers-iio-proximity-sx9324-c-8c3437676039 Best regards, -- Justin Stitt