hwmon: zero-initialization instead of memset
cover hwmon: zero-initialization instead of memset
Hi all,
This patch series cleans up memory initialization across several hardware monitoring (hwmon) drivers by replacing explicit memset() calls with empty brace initialization (= {}).
Following similar cleanups in the IIO subsystem [1], this series updates these drivers to stop using memset() for stack memory initialization. As noted in those discussions [2], using empty brace initialization (= {}) is the preferred approach.
Beyond simple replacements, a few drivers (nct6683, nct6775-platform, it87) were using memset() inside a for-loop. To fix this, the variable declarationwas moved directly inside the loop and zero-initialized there. This safely resets the data on every iteration and makes the code much cleaner.
Testing:
- Compiled all modified files using make W=1 with no warnings or errors.
[1]: https://lore.kernel.org/all/20250611-iio-zero-init-stack-with-instead-of-memset-v1-0-ebb2d0a24302@baylibre.com/
[2]: https://lore.kernel.org/linux-iio/202505090942.48EBF01B@keescook/
Manish Baing (7):
hwmon: drivetemp: Use zero-initialization instead of memset()
hwmon: f71882fg: Use zero-initialization instead of memset()
hwmon: ltc2947-core: Use zero-initialization instead of memset()
hwmon: nct6683: Use zero-initialization instead of memset()
hwmon: nct6775-platform: Use zero-initialization instead of memset()
hwmon: pmbus: adm1266: Use zero-initialization instead of memset()
hwmon: it87: Use zero-initialization instead of memset()
drivers/hwmon/drivetemp.c | 3 +-- drivers/hwmon/f71882fg.c | 4 +--- drivers/hwmon/it87.c | 3 +-- drivers/hwmon/ltc2947-core.c | 4 +--- drivers/hwmon/nct6683.c | 4 ++-- drivers/hwmon/nct6775-platform.c | 4 ++-- drivers/hwmon/pmbus/adm1266.c | 4 +--- 7 files changed, 9 insertions(+), 17 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/7 hwmon: drivetemp: Use zero-initialization instead of memset()
Use empty brace initialization (={}) instead of explicit memset() to zero-initialize stack memory to simplify the code.
No functional change.
--- drivers/hwmon/drivetemp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/hwmon/drivetemp.c +1 -2
@@ -161,11 +161,10 @@ static int drivetemp_scsi_command(struct drivetemp_data *st, u8 ata_command, u8 feature, u8 lba_low, u8 lba_mid, u8 lba_high) { - u8 scsi_cmd[MAX_COMMAND_SIZE]; + u8 scsi_cmd[MAX_COMMAND_SIZE] = { };enum req_op op;int err; -memset (scsi_cmd,0 ,sizeof (scsi_cmd)); scsi_cmd[0 ] =ATA_16 ;if (ata_command ==ATA_CMD_SMART && feature ==SMART_WRITE_LOG ) { scsi_cmd[1 ] = (5 <<1 );/* PIO Data-out */
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 1/7] hwmon: drivetemp: Use zero-initialization instead of memset()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Use empty brace initialization (={}) instead of explicit
memset()to zero-initialize stack memory to simplify the code.No functional change.
Signed-off-by Manish Baing <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH 2/7 hwmon: f71882fg: Use zero-initialization instead of memset()
In reply to Manish BaingUse empty brace initialization (={}) instead of explicit memset() to zero-initialize stack memory to simplify the code.
No functional change.
--- drivers/hwmon/f71882fg.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwmon/f71882fg.c +1 -3
@@ -2667,9 +2667,7 @@ static int __init f71882fg_init(void) {int err;int address; -struct f71882fg_sio_data sio_data; - -memset (&sio_data,0 ,sizeof (sio_data)); +struct f71882fg_sio_data sio_data = { }; address =f71882fg_find (0x2e , &sio_data);if (address <0 )
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 2/7] hwmon: f71882fg: Use zero-initialization instead of memset()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Use empty brace initialization (={}) instead of explicit
memset()to zero-initialize stack memory to simplify the code.No functional change.
Signed-off-by Manish Baing <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/7 hwmon: ltc2947-core: Use zero-initialization instead of memset()
In reply to Manish BaingUse empty brace initialization (={}) instead of explicit memset() to zero-initialize stack memory to simplify the code.
No functional change.
--- drivers/hwmon/ltc2947-core.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwmon/ltc2947-core.c +1 -3
@@ -273,11 +273,9 @@ static int ltc2947_alarm_read(struct ltc2947_data *st, const u8 reg, {u8 offset = reg -LTC2947_REG_STATUS ;/* +1 to include status reg */ -char alarms[LTC2947_ALERTS_SIZE +1 ]; +char alarms[LTC2947_ALERTS_SIZE +1 ] = { };int ret =0 ; -memset (alarms,0 ,sizeof (alarms)); - ret =regmap_write (st->map,LTC2947_REG_PAGE_CTRL ,LTC2947_PAGE0 );if (ret)return ret;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 3/7] hwmon: ltc2947-core: Use zero-initialization instead of memset()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Use empty brace initialization (={}) instead of explicit
memset()to zero-initialize stack memory to simplify the code.No functional change.
Signed-off-by Manish Baing <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/7 hwmon: nct6683: Use zero-initialization instead of memset()
In reply to Manish BaingReduce the scope of 'struct resource res' by moving it inside the for-loop and using empty brace initialization (= {}). This ensures the structure is safely zero-initialized on every iteration, allowing for the safe removal of explicit memset() call.
No functional change.
--- drivers/hwmon/nct6683.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/nct6683.c +2 -2
@@ -1437,7 +1437,6 @@ static int __init sensors_nct6683_init(void) {struct nct6683_sio_data sio_data;int sioaddr[2 ] = {0x2e ,0x4e }; -struct resource res;bool found = false;int address;int i, err;
@@ -1454,6 +1453,8 @@ static int __init sensors_nct6683_init(void)* nct6683 hardware monitor, and call probe() */ for (i =0 ; i <ARRAY_SIZE (pdev); i++) { +struct resource res = { }; + address =nct6683_find (sioaddr[i], &sio_data);if (address <=0 )continue ;
@@ -1471,7 +1472,6 @@ static int __init sensors_nct6683_init(void)if (err) goto exit_device_put; -memset (&res,0 ,sizeof (res)); res.name =DRVNAME ; res.start = address +IOREGION_OFFSET ; res.end = address +IOREGION_OFFSET +IOREGION_LENGTH -1 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 4/7] hwmon: nct6683: Use zero-initialization instead of memset()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
Reduce the scope of 'struct resource res' by moving it inside the for-loop and using empty brace initialization (= {}). This ensures the structure is safely zero-initialized on every iteration, allowing for the safe removal of explicit
memset()call.No functional change.
Signed-off-by Manish Baing <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH 5/7 hwmon: nct6775-platform: Use zero-initialization instead of memset()
In reply to Manish BaingReduce the scope of 'struct resource res' by moving it inside the for-loop and using empty brace initialization (= {}). This ensures the structure is safely zero-initialized on every iteration, allowing for the safe removal of explicit memset() call.
No functional change.
--- drivers/hwmon/nct6775-platform.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hwmon/nct6775-platform.c +2 -2
@@ -1528,7 +1528,6 @@ static int __init sensors_nct6775_platform_init(void)int i, err;bool found = false;int address; -struct resource res;struct nct6775_sio_data sio_data;int sioaddr[2 ] = {0x2e ,0x4e };enum sensor_access access = access_direct;
@@ -1562,6 +1561,8 @@ static int __init sensors_nct6775_platform_init(void)* nct6775 hardware monitor, and call probe() */ for (i =0 ; i <ARRAY_SIZE (pdev); i++) { +struct resource res = { }; + sio_data.sio_outb = superio_outb; sio_data.sio_inb = superio_inb; sio_data.sio_select = superio_select;
@@ -1596,7 +1597,6 @@ static int __init sensors_nct6775_platform_init(void) goto exit_device_put;if (sio_data.access == access_direct) { -memset (&res,0 ,sizeof (res)); res.name =DRVNAME ; res.start = address +IOREGION_OFFSET ; res.end = address +IOREGION_OFFSET +IOREGION_LENGTH -1 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 5/7] hwmon: nct6775-platform: Use zero-initialization instead of memset()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines
Reduce the scope of 'struct resource res' by moving it inside the for-loop and using empty brace initialization (= {}). This ensures the structure is safely zero-initialized on every iteration, allowing for the safe removal of explicit
memset()call.No functional change.
Signed-off-by Manish Baing <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH 6/7 hwmon: pmbus: adm1266: Use zero-initialization instead of memset()
In reply to Manish BaingUse empty brace initialization (={}) instead of explicit memset() to zero-initialize stack memory to simplify the code.
No functional change.
--- drivers/hwmon/pmbus/adm1266.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/hwmon/pmbus/adm1266.c +1 -3
@@ -449,13 +449,11 @@ static int adm1266_config_nvmem(struct adm1266_data *data)static int adm1266_set_rtc (struct adm1266_data *data) {time64_t kt; -char write_buf[6 ]; +char write_buf[6 ] = { };int i; kt =ktime_get_real_seconds (); -memset (write_buf,0 ,sizeof (write_buf)); -for (i =0 ; i <4 ; i++) write_buf[2 + i] = (kt >> (i *8 )) &0xFF ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 6/7] hwmon: pmbus: adm1266: Use zero-initialization instead of memset()
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines
Use empty brace initialization (={}) instead of explicit
memset()to zero-initialize stack memory to simplify the code.No functional change.
Signed-off-by Manish Baing <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH 7/7 hwmon: it87: Use zero-initialization instead of memset()
In reply to Manish BaingReduce the scope of 'struct it87_sio_data sio_data' by moving it inside the for-loop and using empty brace initialization (= {}). This ensures the structure is safely zero-initialized on every iteration, allowing for the safe removal of the explicit memset() call.
No functional change.
--- drivers/hwmon/it87.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/hwmon/it87.c +1 -2
@@ -3777,7 +3777,6 @@ MODULE_DEVICE_TABLE(dmi, it87_dmi_table);static int __init sm_it87_init (void ) {int sioaddr[2 ] = {REG_2E ,REG_4E }; -struct it87_sio_data sio_data;unsigned short isa_address[2 ];bool found = false;int i, err;
@@ -3789,7 +3788,7 @@ static int __init sm_it87_init(void)dmi_check_system (it87_dmi_table);for (i =0 ; i <ARRAY_SIZE (sioaddr); i++) { -memset (&sio_data,0 ,sizeof (struct it87_sio_data )); +struct it87_sio_data sio_data = { }; isa_address[i] =0 ; err =it87_find (sioaddr[i], &isa_address[i], &sio_data, i);if (err || isa_address[i] ==0 )
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 7/7] hwmon: it87: Use zero-initialization instead of memset()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Replacing memset() with empty brace initialization leaves struct padding bytes uninitialized…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Replacing memset() with empty brace initialization leaves struct padding bytes uninitialized, causing a KMSAN uninit-value warning when copied via platform_device_add_data(). --
commit 429f8b305f54cb034b99374e3bdaa61a1ba8f424
Author: Manish Baing <[email protected]>
hwmon: it87: Use zero-initialization instead of memset()
This commit reduces the scope of 'sio_data' by moving it inside the for-loop and replacing the explicit memset() with empty brace initialization.
Quoted 13 lines
drivers/hwmon/it87.c +0
-1@@ -3777,7 +3777,6 @@ MODULE_DEVICE_TABLE(dmi, it87_dmi_table);static int __init sm_it87_init (void ) {int sioaddr[2 ] = {REG_2E ,REG_4E }; -struct it87_sio_data sio_data;unsigned short isa_address[2 ];bool found = false;int i, err;
drivers/hwmon/it87.c static int __init sm_it87_init(void) · line 3791
Quoted 5 lines
drivers/hwmon/it87.c +1
-1@@ -3789,7 +3788,7 @@ static int __init sm_it87_init(void)dmi_check_system (it87_dmi_table);for (i =0 ; i <ARRAY_SIZE (sioaddr); i++) { -memset (&sio_data,0 ,sizeof (struct it87_sio_data )); +struct it87_sio_data sio_data = { };
[Severity: Medium]
Does replacing memset() with empty brace initialization leave struct padding bytes uninitialized?
Empty brace initialization is often lowered by compilers to member-wise zeroing, which skips padding bytes.
When this structure is passed to it87_device_add(), it is copied byte-by-byte via platform_device_add_data():
drivers/hwmon/it87.c:it87_device_add() {
...
err = platform_device_add_data(pdev, sio_data,
sizeof(struct it87_sio_data));
...
}Because platform_device_add_data() uses kmemdup() to copy the entire size of the structure, won't reading the uninitialized padding bytes trigger a KMSAN uninitialized-value warning during driver probe?
Quoted 3 lines
drivers/hwmon/it87.c +0
-0isa_address[i] =0 ; err =it87_find (sioaddr[i], &isa_address[i], &sio_data, i);if (err || isa_address[i] ==0 )
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH 7/7] hwmon: it87: Use zero-initialization instead of memset()
In reply to Manish BaingHi all,
The Sashiko AI bot flagged that replacing memset() with empty brace initialization {} here might leave padding bytes uninitialized.
How would you prefer to proceed? Please let me know in case I need
to send a v2 to address this.
Sashiko AI review ·
https://sashiko.dev/#/patchset/[email protected]?part=7
Thanks & Regards,
Manish
Quoted 37 lines · Manish Baing
Reduce the scope of 'struct
it87_sio_datasio_data' by moving it inside the for-loop and using empty brace initialization (= {}). This ensures the structure is safely zero-initialized on every iteration, allowing for the safe removal of the explicitmemset()call.No functional change.
Signed-off-by Manish Baing <[email protected]>--- drivers/hwmon/it87.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/hwmon/it87.c +1
-3@@ -3777,7 +3777,6 @@ MODULE_DEVICE_TABLE(dmi, it87_dmi_table);static int __init sm_it87_init (void ) {int sioaddr[2 ] = {REG_2E ,REG_4E }; -struct it87_sio_data sio_data;unsigned short isa_address[2 ];bool found = false;int i, err;@@ -3789,7 +3788,7 @@ static int __init sm_it87_init(void)dmi_check_system (it87_dmi_table);for (i =0 ; i <ARRAY_SIZE (sioaddr); i++) { -memset (&sio_data,0 ,sizeof (struct it87_sio_data )); +struct it87_sio_data sio_data = { }; isa_address[i] =0 ; err =it87_find (sioaddr[i], &isa_address[i], &sio_data, i);if (err || isa_address[i] ==0 ) --2.43.0
Guenter Roeck
maintainerQuoted 17 lines · Manish Baing
Hi all,
This patch series cleans up memory initialization across several hardware monitoring (hwmon) drivers by replacing explicit
memset()calls with empty brace initialization (= {}).Following similar cleanups in the IIO subsystem [1], this series updates these drivers to stop using
memset()for stack memory initialization. As noted in those discussions [2], using empty brace initialization (= {}) is the preferred approach.Beyond simple replacements, a few drivers (nct6683, nct6775-platform, it87) were using
memset()inside a for-loop. To fix this, the variable declarationwas moved directly inside the loop and zero-initialized there. This safely resets the data on every iteration and makes the code much cleaner.
Quoted 5 lines
Testing:
- Compiled all modified files usingmake W=1with no warnings or errors.[1]: https://lore.kernel.org/all/20250611-iio-zero-init-stack-with-instead-of-memset-v1-0-ebb2d0a24302@baylibre.com/
[2]: https://lore.kernel.org/linux-iio/202505090942.48EBF01B@keescook/
That discussion does not fully address a problem I had seen previously, where {} did _not_ zero-fill all holes, causing subsequent memcmp() failures on affected data structures. It took a lot a lot of debugging to find and fix that problem. Given that, I am not inclined to accept this series unless someone convinces me that it fixes an actual problem _and_ that each and every instance of the changes is not used in a subsequent memcmp().
Guenter
Hi Guenter,
Thank you for catching the struct padding risk. You are completely right—Patch 7 (it87.c) would trigger a KMSAN uninit-value warning due to the kmemdup() copy in platform_device_add_data(). I will drop that patch entirely.
I have audited the other six drivers in this series and verified that their variables are never passed to memcmp(), kmemdup(), or hashed, so the = {} initialization is perfectly safe there.
While this does not fix an active bug, the goal is to follow a cleanup pattern recently discussed for the IIO subsystem [1],[2] 1.Better performance: It removes the need for an extra function call. Instead, the compiler clears the memory directly and more efficiently. 2.Safer code: It prevents accidental memory leaks that happen when developers make a typo in the sizeof() calculation.
Would you be open to a v2 that includes just the six safe patches to modernize these initializations?
[1] https://lore.kernel.org/all/20250611-iio-zero-init-stack-with-instead-of-memset-v1-0-ebb2d0a24302@baylibre.com/
[2] https://lore.kernel.org/linux-iio/202505090942.48EBF01B@keescook/
Thanks for your time and guidance,
Thanks & Regards,
Manish
Quoted 35 lines · Guenter Roeck
Quoted 17 lines · Manish Baing
Hi all,
This patch series cleans up memory initialization across several hardware monitoring (hwmon) drivers by replacing explicit
memset()calls with empty brace initialization (= {}).Following similar cleanups in the IIO subsystem [1], this series updates these drivers to stop using
memset()for stack memory initialization. As noted in those discussions [2], using empty brace initialization (= {}) is the preferred approach.Beyond simple replacements, a few drivers (nct6683, nct6775-platform, it87) were using
memset()inside a for-loop. To fix this, the variable declarationwas moved directly inside the loop and zero-initialized there. This safely resets the data on every iteration and makes the code much cleaner.Quoted 5 lines
Testing:
- Compiled all modified files usingmake W=1with no warnings or errors.[1]: https://lore.kernel.org/all/20250611-iio-zero-init-stack-with-instead-of-memset-v1-0-ebb2d0a24302@baylibre.com/
[2]: https://lore.kernel.org/linux-iio/202505090942.48EBF01B@keescook/That discussion does not fully address a problem I had seen previously, where {} did _not_ zero-fill all holes, causing subsequent
memcmp()failures on affected data structures. It took a lot a lot of debugging to find and fix that problem. Given that, I am not inclined to accept this series unless someone convinces me that it fixes an actual problem _and_ that each and every instance of the changes is not used in a subsequentmemcmp().Guenter
Guenter Roeck
maintainerQuoted 18 lines · Manish Baing
Hi Guenter,
Thank you for catching the struct padding risk. You are completely right—Patch 7 (
it87.c) would trigger a KMSAN uninit-value warning due to thekmemdup()copy inplatform_device_add_data(). I will drop that patch entirely.I have audited the other six drivers in this series and verified that their variables are never passed to
memcmp(),kmemdup(), or hashed, so the = {} initialization is perfectly safe there.While this does not fix an active bug, the goal is to follow a cleanup pattern recently discussed for the IIO subsystem [1],[2] 1.Better performance: It removes the need for an extra function call. Instead, the compiler clears the memory directly and more efficiently. 2.Safer code: It prevents accidental memory leaks that happen when developers make a typo in the
sizeof()calculation.
Quoted 3 lines
Would you be open to a v2 that includes just the six safe patches to modernize these initializations?
Not really. I would not mind if there was a safe means to initialize a data structure without risking holes and without memset(). In the absence of that I rather play safe than sorry (and apparently we have a different opinion about the definition of "safe").
Guenter
Quoted 45 lines
[1] https://lore.kernel.org/all/20250611-iio-zero-init-stack-with-instead-of-memset-v1-0-ebb2d0a24302@baylibre.com/
[2] https://lore.kernel.org/linux-iio/202505090942.48EBF01B@keescook/Thanks for your time and guidance,
Thanks & Regards,
ManishQuoted 35 lines · Guenter Roeck
Quoted 17 lines · Manish Baing
Hi all,
This patch series cleans up memory initialization across several hardware monitoring (hwmon) drivers by replacing explicit
memset()calls with empty brace initialization (= {}).Following similar cleanups in the IIO subsystem [1], this series updates these drivers to stop using
memset()for stack memory initialization. As noted in those discussions [2], using empty brace initialization (= {}) is the preferred approach.Beyond simple replacements, a few drivers (nct6683, nct6775-platform, it87) were using
memset()inside a for-loop. To fix this, the variable declarationwas moved directly inside the loop and zero-initialized there. This safely resets the data on every iteration and makes the code much cleaner.Quoted 5 lines
Testing:
- Compiled all modified files usingmake W=1with no warnings or errors.[1]: https://lore.kernel.org/all/20250611-iio-zero-init-stack-with-instead-of-memset-v1-0-ebb2d0a24302@baylibre.com/
[2]: https://lore.kernel.org/linux-iio/202505090942.48EBF01B@keescook/That discussion does not fully address a problem I had seen previously, where {} did _not_ zero-fill all holes, causing subsequent
memcmp()failures on affected data structures. It took a lot a lot of debugging to find and fix that problem. Given that, I am not inclined to accept this series unless someone convinces me that it fixes an actual problem _and_ that each and every instance of the changes is not used in a subsequentmemcmp().Guenter
Hi Guenter,
That makes complete sense.
I understand the risks of leaving padding bytes uninitialized now. Thank you for taking the time to explain the rationale! it is a very valuable learning experience.
Thanks & Regards
Manish Baing
Quoted 77 lines · Guenter Roeck
Quoted 18 lines · Manish Baing
Hi Guenter,
Thank you for catching the struct padding risk. You are completely right—Patch 7 (
it87.c) would trigger a KMSAN uninit-value warning due to thekmemdup()copy inplatform_device_add_data(). I will drop that patch entirely.I have audited the other six drivers in this series and verified that their variables are never passed to
memcmp(),kmemdup(), or hashed, so the = {} initialization is perfectly safe there.While this does not fix an active bug, the goal is to follow a cleanup pattern recently discussed for the IIO subsystem [1],[2] 1.Better performance: It removes the need for an extra function call. Instead, the compiler clears the memory directly and more efficiently. 2.Safer code: It prevents accidental memory leaks that happen when developers make a typo in the
sizeof()calculation.Quoted 3 lines
Would you be open to a v2 that includes just the six safe patches to modernize these initializations?
Not really. I would not mind if there was a safe means to initialize a data structure without risking holes and without
memset(). In the absence of that I rather play safe than sorry (and apparently we have a different opinion about the definition of "safe").Guenter
Quoted 45 lines
[1] https://lore.kernel.org/all/20250611-iio-zero-init-stack-with-instead-of-memset-v1-0-ebb2d0a24302@baylibre.com/
[2] https://lore.kernel.org/linux-iio/202505090942.48EBF01B@keescook/Thanks for your time and guidance,
Thanks & Regards,
ManishQuoted 35 lines · Guenter Roeck
Quoted 17 lines · Manish Baing
Hi all,
This patch series cleans up memory initialization across several hardware monitoring (hwmon) drivers by replacing explicit
memset()calls with empty brace initialization (= {}).Following similar cleanups in the IIO subsystem [1], this series updates these drivers to stop using
memset()for stack memory initialization. As noted in those discussions [2], using empty brace initialization (= {}) is the preferred approach.Beyond simple replacements, a few drivers (nct6683, nct6775-platform, it87) were using
memset()inside a for-loop. To fix this, the variable declarationwas moved directly inside the loop and zero-initialized there. This safely resets the data on every iteration and makes the code much cleaner.Quoted 5 lines
Testing:
- Compiled all modified files usingmake W=1with no warnings or errors.[1]: https://lore.kernel.org/all/20250611-iio-zero-init-stack-with-instead-of-memset-v1-0-ebb2d0a24302@baylibre.com/
[2]: https://lore.kernel.org/linux-iio/202505090942.48EBF01B@keescook/That discussion does not fully address a problem I had seen previously, where {} did _not_ zero-fill all holes, causing subsequent
memcmp()failures on affected data structures. It took a lot a lot of debugging to find and fix that problem. Given that, I am not inclined to accept this series unless someone convinces me that it fixes an actual problem _and_ that each and every instance of the changes is not used in a subsequentmemcmp().Guenter