Skip to content

Commit 48cf49d

Browse files
tititiou36rafaeljw
authored andcommitted
ACPI: sysfs: Fix create_pnp_modalias() and create_of_modalias()
snprintf() does not return negative values on error. To know if the buffer was too small, the returned value needs to be compared with the length of the passed buffer. If it is greater or equal, the output has been truncated, so add checks for the truncation to create_pnp_modalias() and create_of_modalias(). Also make them return -ENOMEM in that case, as they already do that elsewhere. Moreover, the remaining size of the buffer used by snprintf() needs to be updated after the first write to avoid out-of-bounds access as already done correctly in create_pnp_modalias(), but not in create_of_modalias(), so change the latter accordingly. Fixes: 8765c5b ("ACPI / scan: Rework modalias creation when "compatible" is present") Signed-off-by: Christophe JAILLET <[email protected]> [ rjw: Merge two patches into one, combine changelogs, add subject ] Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 05d3ef8 commit 48cf49d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

drivers/acpi/device_sysfs.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ static int create_pnp_modalias(const struct acpi_device *acpi_dev, char *modalia
158158
return 0;
159159

160160
len = snprintf(modalias, size, "acpi:");
161-
if (len <= 0)
162-
return len;
161+
if (len >= size)
162+
return -ENOMEM;
163163

164164
size -= len;
165165

@@ -212,8 +212,10 @@ static int create_of_modalias(const struct acpi_device *acpi_dev, char *modalias
212212
len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
213213
ACPI_FREE(buf.pointer);
214214

215-
if (len <= 0)
216-
return len;
215+
if (len >= size)
216+
return -ENOMEM;
217+
218+
size -= len;
217219

218220
of_compatible = acpi_dev->data.of_compatible;
219221
if (of_compatible->type == ACPI_TYPE_PACKAGE) {

0 commit comments

Comments
 (0)