Skip to content

Commit 48b557d

Browse files
committed
libkbdfile: avoid double formatting in pathname helper
kbdfile_pathname_sprintf() currently formats the same string twice to compute size and then write output. This adds unnecessary work in a path-building helper that is used repeatedly during file lookup. Signed-off-by: Alexey Gladkov <legion@kernel.org>
1 parent bb83f84 commit 48b557d

1 file changed

Lines changed: 2 additions & 6 deletions

File tree

src/libkbdfile/kbdfile.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,19 @@ kbdfile_set_pathname(struct kbdfile *fp, const char *pathname)
8484
static int KBD_ATTR_PRINTF(2, 3)
8585
kbdfile_pathname_sprintf(struct kbdfile *fp, const char *fmt, ...)
8686
{
87-
ssize_t size;
87+
int size;
8888
va_list ap;
8989

9090
if (fp == NULL || fmt == NULL)
9191
return -1;
9292

9393
va_start(ap, fmt);
94-
size = vsnprintf(NULL, 0, fmt, ap);
94+
size = vsnprintf(fp->pathname, sizeof(fp->pathname), fmt, ap);
9595
va_end(ap);
9696

9797
if (size < 0 || (size_t) size >= sizeof(fp->pathname))
9898
return -1;
9999

100-
va_start(ap, fmt);
101-
vsnprintf(fp->pathname, sizeof(fp->pathname), fmt, ap);
102-
va_end(ap);
103-
104100
return 0;
105101
}
106102

0 commit comments

Comments
 (0)