Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: sx9324: avoid copying property strings

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].

Furthermore, let's clean up this code further by removing some unused
defines:
| #define SX9324_PIN_DEF "semtech,ph0-pin"
| #define SX9324_RESOLUTION_DEF "semtech,ph01-resolution"
| #define SX9324_PROXRAW_DEF "semtech,ph01-proxraw-strength"

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 <justinstitt@google.com>
Reviewed-by: Stephen Boyd <swboyd@chromium.org>
Link: https://lore.kernel.org/r/20240201-strncpy-drivers-iio-proximity-sx9324-c-v5-1-78dde23553bc@google.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Justin Stitt and committed by
Jonathan Cameron
f7b6443f c8f883a7

+40 -29
+40 -29
drivers/iio/proximity/sx9324.c
··· 873 873 20000, 2000000); 874 874 } 875 875 876 + static u8 sx9324_parse_phase_prop(struct device *dev, 877 + struct sx_common_reg_default *reg_def, 878 + const char *prop) 879 + { 880 + unsigned int pin_defs[SX9324_NUM_PINS]; 881 + int count, ret, pin; 882 + u32 raw = 0; 883 + 884 + count = device_property_count_u32(dev, prop); 885 + if (count != ARRAY_SIZE(pin_defs)) 886 + return reg_def->def; 887 + ret = device_property_read_u32_array(dev, prop, pin_defs, 888 + ARRAY_SIZE(pin_defs)); 889 + if (ret) 890 + return reg_def->def; 891 + 892 + for (pin = 0; pin < SX9324_NUM_PINS; pin++) 893 + raw |= (pin_defs[pin] << (2 * pin)) & 894 + SX9324_REG_AFE_PH0_PIN_MASK(pin); 895 + 896 + return raw; 897 + } 898 + 876 899 static const struct sx_common_reg_default * 877 900 sx9324_get_default_reg(struct device *dev, int idx, 878 901 struct sx_common_reg_default *reg_def) ··· 904 881 "highest" }; 905 882 static const char * const sx9324_csidle[] = { "hi-z", "hi-z", "gnd", 906 883 "vdd" }; 907 - #define SX9324_PIN_DEF "semtech,ph0-pin" 908 - #define SX9324_RESOLUTION_DEF "semtech,ph01-resolution" 909 - #define SX9324_PROXRAW_DEF "semtech,ph01-proxraw-strength" 910 - unsigned int pin_defs[SX9324_NUM_PINS]; 911 - char prop[] = SX9324_PROXRAW_DEF; 912 884 u32 start = 0, raw = 0, pos = 0; 913 - int ret, count, ph, pin; 885 + const char *prop; 886 + int ret; 914 887 915 888 memcpy(reg_def, &sx9324_default_regs[idx], sizeof(*reg_def)); 916 889 917 890 sx_common_get_raw_register_config(dev, reg_def); 918 891 switch (reg_def->reg) { 919 892 case SX9324_REG_AFE_PH0: 893 + reg_def->def = sx9324_parse_phase_prop(dev, reg_def, 894 + "semtech,ph0-pin"); 895 + break; 920 896 case SX9324_REG_AFE_PH1: 897 + reg_def->def = sx9324_parse_phase_prop(dev, reg_def, 898 + "semtech,ph1-pin"); 899 + break; 921 900 case SX9324_REG_AFE_PH2: 901 + reg_def->def = sx9324_parse_phase_prop(dev, reg_def, 902 + "semtech,ph2-pin"); 903 + break; 922 904 case SX9324_REG_AFE_PH3: 923 - ph = reg_def->reg - SX9324_REG_AFE_PH0; 924 - snprintf(prop, ARRAY_SIZE(prop), "semtech,ph%d-pin", ph); 925 - 926 - count = device_property_count_u32(dev, prop); 927 - if (count != ARRAY_SIZE(pin_defs)) 928 - break; 929 - ret = device_property_read_u32_array(dev, prop, pin_defs, 930 - ARRAY_SIZE(pin_defs)); 931 - if (ret) 932 - break; 933 - 934 - for (pin = 0; pin < SX9324_NUM_PINS; pin++) 935 - raw |= (pin_defs[pin] << (2 * pin)) & 936 - SX9324_REG_AFE_PH0_PIN_MASK(pin); 937 - reg_def->def = raw; 905 + reg_def->def = sx9324_parse_phase_prop(dev, reg_def, 906 + "semtech,ph3-pin"); 938 907 break; 939 908 case SX9324_REG_AFE_CTRL0: 940 909 ret = device_property_match_property_string(dev, "semtech,cs-idle-sleep", ··· 948 933 case SX9324_REG_AFE_CTRL4: 949 934 case SX9324_REG_AFE_CTRL7: 950 935 if (reg_def->reg == SX9324_REG_AFE_CTRL4) 951 - strncpy(prop, "semtech,ph01-resolution", 952 - ARRAY_SIZE(prop)); 936 + prop = "semtech,ph01-resolution"; 953 937 else 954 - strncpy(prop, "semtech,ph23-resolution", 955 - ARRAY_SIZE(prop)); 938 + prop = "semtech,ph23-resolution"; 956 939 957 940 ret = device_property_read_u32(dev, prop, &raw); 958 941 if (ret) ··· 1021 1008 case SX9324_REG_PROX_CTRL0: 1022 1009 case SX9324_REG_PROX_CTRL1: 1023 1010 if (reg_def->reg == SX9324_REG_PROX_CTRL0) 1024 - strncpy(prop, "semtech,ph01-proxraw-strength", 1025 - ARRAY_SIZE(prop)); 1011 + prop = "semtech,ph01-proxraw-strength"; 1026 1012 else 1027 - strncpy(prop, "semtech,ph23-proxraw-strength", 1028 - ARRAY_SIZE(prop)); 1013 + prop = "semtech,ph23-proxraw-strength"; 1029 1014 ret = device_property_read_u32(dev, prop, &raw); 1030 1015 if (ret) 1031 1016 break;