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

mtd: core: Convert sysfs sprintf/snprintf family to sysfs_emit

Use sysfs_emit instead of snprintf to avoid buf overrun,because in
sysfs_emit it strictly checks whether buf is null or buf whether
pagesize aligned, otherwise it returns an error.

Signed-off-by: Tian Tao <tiantao6@hisilicon.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/1618220144-33839-2-git-send-email-tiantao6@hisilicon.com

authored by

Tian Tao and committed by
Miquel Raynal
5b2fbe0c e4f3c911

+17 -18
+17 -18
drivers/mtd/mtdcore.c
··· 131 131 type = "unknown"; 132 132 } 133 133 134 - return snprintf(buf, PAGE_SIZE, "%s\n", type); 134 + return sysfs_emit(buf, "%s\n", type); 135 135 } 136 136 static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL); 137 137 ··· 140 140 { 141 141 struct mtd_info *mtd = dev_get_drvdata(dev); 142 142 143 - return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags); 143 + return sysfs_emit(buf, "0x%lx\n", (unsigned long)mtd->flags); 144 144 } 145 145 static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL); 146 146 ··· 149 149 { 150 150 struct mtd_info *mtd = dev_get_drvdata(dev); 151 151 152 - return snprintf(buf, PAGE_SIZE, "%llu\n", 153 - (unsigned long long)mtd->size); 152 + return sysfs_emit(buf, "%llu\n", (unsigned long long)mtd->size); 154 153 } 155 154 static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL); 156 155 ··· 158 159 { 159 160 struct mtd_info *mtd = dev_get_drvdata(dev); 160 161 161 - return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize); 162 + return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->erasesize); 162 163 } 163 164 static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL); 164 165 ··· 167 168 { 168 169 struct mtd_info *mtd = dev_get_drvdata(dev); 169 170 170 - return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize); 171 + return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->writesize); 171 172 } 172 173 static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL); 173 174 ··· 177 178 struct mtd_info *mtd = dev_get_drvdata(dev); 178 179 unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft; 179 180 180 - return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize); 181 + return sysfs_emit(buf, "%u\n", subpagesize); 181 182 } 182 183 static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL); 183 184 ··· 186 187 { 187 188 struct mtd_info *mtd = dev_get_drvdata(dev); 188 189 189 - return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize); 190 + return sysfs_emit(buf, "%lu\n", (unsigned long)mtd->oobsize); 190 191 } 191 192 static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL); 192 193 ··· 195 196 { 196 197 struct mtd_info *mtd = dev_get_drvdata(dev); 197 198 198 - return snprintf(buf, PAGE_SIZE, "%u\n", mtd->oobavail); 199 + return sysfs_emit(buf, "%u\n", mtd->oobavail); 199 200 } 200 201 static DEVICE_ATTR(oobavail, S_IRUGO, mtd_oobavail_show, NULL); 201 202 ··· 204 205 { 205 206 struct mtd_info *mtd = dev_get_drvdata(dev); 206 207 207 - return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions); 208 + return sysfs_emit(buf, "%u\n", mtd->numeraseregions); 208 209 } 209 210 static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show, 210 211 NULL); ··· 214 215 { 215 216 struct mtd_info *mtd = dev_get_drvdata(dev); 216 217 217 - return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name); 218 + return sysfs_emit(buf, "%s\n", mtd->name); 218 219 } 219 220 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL); 220 221 ··· 223 224 { 224 225 struct mtd_info *mtd = dev_get_drvdata(dev); 225 226 226 - return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength); 227 + return sysfs_emit(buf, "%u\n", mtd->ecc_strength); 227 228 } 228 229 static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL); 229 230 ··· 233 234 { 234 235 struct mtd_info *mtd = dev_get_drvdata(dev); 235 236 236 - return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold); 237 + return sysfs_emit(buf, "%u\n", mtd->bitflip_threshold); 237 238 } 238 239 239 240 static ssize_t mtd_bitflip_threshold_store(struct device *dev, ··· 260 261 { 261 262 struct mtd_info *mtd = dev_get_drvdata(dev); 262 263 263 - return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size); 264 + return sysfs_emit(buf, "%u\n", mtd->ecc_step_size); 264 265 265 266 } 266 267 static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL); ··· 271 272 struct mtd_info *mtd = dev_get_drvdata(dev); 272 273 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 273 274 274 - return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->corrected); 275 + return sysfs_emit(buf, "%u\n", ecc_stats->corrected); 275 276 } 276 277 static DEVICE_ATTR(corrected_bits, S_IRUGO, 277 278 mtd_ecc_stats_corrected_show, NULL); ··· 282 283 struct mtd_info *mtd = dev_get_drvdata(dev); 283 284 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 284 285 285 - return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->failed); 286 + return sysfs_emit(buf, "%u\n", ecc_stats->failed); 286 287 } 287 288 static DEVICE_ATTR(ecc_failures, S_IRUGO, mtd_ecc_stats_errors_show, NULL); 288 289 ··· 292 293 struct mtd_info *mtd = dev_get_drvdata(dev); 293 294 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 294 295 295 - return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->badblocks); 296 + return sysfs_emit(buf, "%u\n", ecc_stats->badblocks); 296 297 } 297 298 static DEVICE_ATTR(bad_blocks, S_IRUGO, mtd_badblocks_show, NULL); 298 299 ··· 302 303 struct mtd_info *mtd = dev_get_drvdata(dev); 303 304 struct mtd_ecc_stats *ecc_stats = &mtd->ecc_stats; 304 305 305 - return snprintf(buf, PAGE_SIZE, "%u\n", ecc_stats->bbtblocks); 306 + return sysfs_emit(buf, "%u\n", ecc_stats->bbtblocks); 306 307 } 307 308 static DEVICE_ATTR(bbt_blocks, S_IRUGO, mtd_bbtblocks_show, NULL); 308 309