Skip to content

Commit 0623682

Browse files
tiwaiwilldeacon
authored andcommitted
perf: arm-ccn: Use scnprintf() for robustness
snprintf() is a hard-to-use function, it's especially difficult to use it for concatenating substrings in a buffer with a limited size. Since snprintf() returns the would-be-output size, not the actual size, the subsequent use of snprintf() may point to the incorrect position easily. Although the current code doesn't actually overflow the buffer, it's an incorrect usage. This patch replaces such snprintf() calls with a safer version, scnprintf(). Acked-by: Mark Rutland <[email protected]> Signed-off-by: Takashi Iwai <[email protected]> Signed-off-by: Will Deacon <[email protected]>
1 parent 29cc4ce commit 0623682

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

drivers/perf/arm-ccn.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,41 +328,41 @@ static ssize_t arm_ccn_pmu_event_show(struct device *dev,
328328
struct arm_ccn_pmu_event, attr);
329329
ssize_t res;
330330

331-
res = snprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
331+
res = scnprintf(buf, PAGE_SIZE, "type=0x%x", event->type);
332332
if (event->event)
333-
res += snprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
333+
res += scnprintf(buf + res, PAGE_SIZE - res, ",event=0x%x",
334334
event->event);
335335
if (event->def)
336-
res += snprintf(buf + res, PAGE_SIZE - res, ",%s",
336+
res += scnprintf(buf + res, PAGE_SIZE - res, ",%s",
337337
event->def);
338338
if (event->mask)
339-
res += snprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
339+
res += scnprintf(buf + res, PAGE_SIZE - res, ",mask=0x%x",
340340
event->mask);
341341

342342
/* Arguments required by an event */
343343
switch (event->type) {
344344
case CCN_TYPE_CYCLES:
345345
break;
346346
case CCN_TYPE_XP:
347-
res += snprintf(buf + res, PAGE_SIZE - res,
347+
res += scnprintf(buf + res, PAGE_SIZE - res,
348348
",xp=?,vc=?");
349349
if (event->event == CCN_EVENT_WATCHPOINT)
350-
res += snprintf(buf + res, PAGE_SIZE - res,
350+
res += scnprintf(buf + res, PAGE_SIZE - res,
351351
",port=?,dir=?,cmp_l=?,cmp_h=?,mask=?");
352352
else
353-
res += snprintf(buf + res, PAGE_SIZE - res,
353+
res += scnprintf(buf + res, PAGE_SIZE - res,
354354
",bus=?");
355355

356356
break;
357357
case CCN_TYPE_MN:
358-
res += snprintf(buf + res, PAGE_SIZE - res, ",node=%d", ccn->mn_id);
358+
res += scnprintf(buf + res, PAGE_SIZE - res, ",node=%d", ccn->mn_id);
359359
break;
360360
default:
361-
res += snprintf(buf + res, PAGE_SIZE - res, ",node=?");
361+
res += scnprintf(buf + res, PAGE_SIZE - res, ",node=?");
362362
break;
363363
}
364364

365-
res += snprintf(buf + res, PAGE_SIZE - res, "\n");
365+
res += scnprintf(buf + res, PAGE_SIZE - res, "\n");
366366

367367
return res;
368368
}

0 commit comments

Comments
 (0)