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

PCI/sysfs: Rely on lengths from scnprintf(), dsm_label_utf16s_to_utf8s()

scnprintf() returns the number of bytes written into the buffer. Change
dsm_label_utf16s_to_utf8s() to do the same. Rely on those values instead
of using strlen() to compute the buffer length.

No functional change intended.

[bhelgaas: reorder patch in series, len++ to include newline added by
dsm_label_utf16s_to_utf8s(), commit log]
Link: https://lore.kernel.org/r/20210603000112.703037-3-kw@linux.com
Signed-off-by: Krzysztof Wilczyński <kw@linux.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Logan Gunthorpe <logang@deltatee.com>

authored by

Krzysztof Wilczyński and committed by
Bjorn Helgaas
316ae330 bdcdaa13

+10 -8
+10 -8
drivers/pci/pci-label.c
··· 139 139 ACPI_ATTR_INDEX_SHOW, 140 140 }; 141 141 142 - static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf) 142 + static int dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf) 143 143 { 144 144 int len; 145 + 145 146 len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer, 146 147 obj->buffer.length, 147 148 UTF16_LITTLE_ENDIAN, 148 149 buf, PAGE_SIZE - 1); 149 - buf[len] = '\n'; 150 + buf[len++] = '\n'; 151 + 152 + return len; 150 153 } 151 154 152 155 static int dsm_get_label(struct device *dev, char *buf, ··· 157 154 { 158 155 acpi_handle handle = ACPI_HANDLE(dev); 159 156 union acpi_object *obj, *tmp; 160 - int len = -1; 157 + int len = 0; 161 158 162 159 if (!handle) 163 160 return -1; ··· 178 175 * this entry must return a null string. 179 176 */ 180 177 if (attr == ACPI_ATTR_INDEX_SHOW) { 181 - scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value); 178 + len = scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value); 182 179 } else if (attr == ACPI_ATTR_LABEL_SHOW) { 183 180 if (tmp[1].type == ACPI_TYPE_STRING) 184 - scnprintf(buf, PAGE_SIZE, "%s\n", 181 + len = scnprintf(buf, PAGE_SIZE, "%s\n", 185 182 tmp[1].string.pointer); 186 183 else if (tmp[1].type == ACPI_TYPE_BUFFER) 187 - dsm_label_utf16s_to_utf8s(tmp + 1, buf); 184 + len = dsm_label_utf16s_to_utf8s(tmp + 1, buf); 188 185 } 189 - len = strlen(buf) > 0 ? strlen(buf) : -1; 190 186 } 191 187 192 188 ACPI_FREE(obj); 193 189 194 - return len; 190 + return len > 0 ? len : -1; 195 191 } 196 192 197 193 static ssize_t label_show(struct device *dev, struct device_attribute *attr,