Skip to content

Commit bd3d149

Browse files
andy-shevlag-linaro
authored andcommitted
leds: pca955x: Avoid potential overflow when filling default_label
GCC compiler (Debian 14.2.0-17) is not happy about printing into a too short buffer (when build with `make W=1`): drivers/leds/leds-pca955x.c:554:33: note: ‘snprintf’ output between 2 and 12 bytes into a destination of size 8 Indeed, the buffer size is chosen based on some assumptions, while in general the assigned value might not fit (GCC can't prove it does). Fix this by changing the bits field in the struct pca955x_chipdef to u8, with a positive side effect of the better memory footprint, and convert loop iterator to be unsigned. With that done, update format specifiers accordingly. In one case join back string literal as it improves the grepping over the code based on the message and remove duplicating information (the driver name is printed as pert of the dev_*() output [1]) as we touch the same line anyway. Link: https://lore.kernel.org/r/[email protected]/ [1] Signed-off-by: Andy Shevchenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Lee Jones <[email protected]>
1 parent e35ca99 commit bd3d149

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

drivers/leds/leds-pca955x.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ enum pca955x_type {
7373
};
7474

7575
struct pca955x_chipdef {
76-
int bits;
76+
u8 bits;
7777
u8 slv_addr; /* 7-bit slave address mask */
7878
int slv_addr_shift; /* Number of bits to ignore */
7979
int blink_div; /* PSC divider */
@@ -142,13 +142,13 @@ struct pca955x_platform_data {
142142
};
143143

144144
/* 8 bits per input register */
145-
static inline int pca955x_num_input_regs(int bits)
145+
static inline u8 pca955x_num_input_regs(u8 bits)
146146
{
147147
return (bits + 7) / 8;
148148
}
149149

150150
/* 4 bits per LED selector register */
151-
static inline int pca955x_num_led_regs(int bits)
151+
static inline u8 pca955x_num_led_regs(u8 bits)
152152
{
153153
return (bits + 3) / 4;
154154
}
@@ -581,14 +581,14 @@ static int pca955x_probe(struct i2c_client *client)
581581
struct led_classdev *led;
582582
struct led_init_data init_data;
583583
struct i2c_adapter *adapter;
584-
int i, bit, err, nls, reg;
584+
u8 i, nls, psc0;
585585
u8 ls1[4];
586586
u8 ls2[4];
587587
struct pca955x_platform_data *pdata;
588-
u8 psc0;
589588
bool keep_psc0 = false;
590589
bool set_default_label = false;
591590
char default_label[8];
591+
int bit, err, reg;
592592

593593
chip = i2c_get_match_data(client);
594594
if (!chip)
@@ -610,16 +610,15 @@ static int pca955x_probe(struct i2c_client *client)
610610
return -ENODEV;
611611
}
612612

613-
dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
614-
"slave address 0x%02x\n", client->name, chip->bits,
615-
client->addr);
613+
dev_info(&client->dev, "Using %s %u-bit LED driver at slave address 0x%02x\n",
614+
client->name, chip->bits, client->addr);
616615

617616
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
618617
return -EIO;
619618

620619
if (pdata->num_leds != chip->bits) {
621620
dev_err(&client->dev,
622-
"board info claims %d LEDs on a %d-bit chip\n",
621+
"board info claims %d LEDs on a %u-bit chip\n",
623622
pdata->num_leds, chip->bits);
624623
return -ENODEV;
625624
}
@@ -694,8 +693,7 @@ static int pca955x_probe(struct i2c_client *client)
694693
}
695694

696695
if (set_default_label) {
697-
snprintf(default_label, sizeof(default_label),
698-
"%d", i);
696+
snprintf(default_label, sizeof(default_label), "%u", i);
699697
init_data.default_label = default_label;
700698
} else {
701699
init_data.default_label = NULL;

0 commit comments

Comments
 (0)