Skip to content

Commit dfaf20a

Browse files
jones-drewavpatel
authored andcommitted
KVM: arm64: selftests: Replace str_with_index with strdup_printf
The original author of aarch64/get-reg-list.c (me) was wearing tunnel vision goggles when implementing str_with_index(). There's no reason to have such a special case string function. Instead, take inspiration from glib and implement strdup_printf. The implementation builds on vasprintf() which requires _GNU_SOURCE, but we require _GNU_SOURCE in most files already. Signed-off-by: Andrew Jones <[email protected]> Signed-off-by: Haibo Xu <[email protected]> Signed-off-by: Anup Patel <[email protected]>
1 parent 630b4ce commit dfaf20a

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

tools/testing/selftests/kvm/aarch64/get-reg-list.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,6 @@ static bool check_supported_feat_reg(struct kvm_vcpu *vcpu, __u64 reg)
180180
return true;
181181
}
182182

183-
static const char *str_with_index(const char *template, __u64 index)
184-
{
185-
char *str, *p;
186-
int n;
187-
188-
str = strdup(template);
189-
p = strstr(str, "##");
190-
n = sprintf(p, "%lld", index);
191-
strcat(p + n, strstr(template, "##") + 2);
192-
193-
return (const char *)str;
194-
}
195-
196183
#define REG_MASK (KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_COPROC_MASK)
197184

198185
#define CORE_REGS_XX_NR_WORDS 2
@@ -211,7 +198,7 @@ static const char *core_id_to_str(struct vcpu_config *c, __u64 id)
211198
KVM_REG_ARM_CORE_REG(regs.regs[30]):
212199
idx = (core_off - KVM_REG_ARM_CORE_REG(regs.regs[0])) / CORE_REGS_XX_NR_WORDS;
213200
TEST_ASSERT(idx < 31, "%s: Unexpected regs.regs index: %lld", config_name(c), idx);
214-
return str_with_index("KVM_REG_ARM_CORE_REG(regs.regs[##])", idx);
201+
return strdup_printf("KVM_REG_ARM_CORE_REG(regs.regs[%lld])", idx);
215202
case KVM_REG_ARM_CORE_REG(regs.sp):
216203
return "KVM_REG_ARM_CORE_REG(regs.sp)";
217204
case KVM_REG_ARM_CORE_REG(regs.pc):
@@ -226,12 +213,12 @@ static const char *core_id_to_str(struct vcpu_config *c, __u64 id)
226213
KVM_REG_ARM_CORE_REG(spsr[KVM_NR_SPSR - 1]):
227214
idx = (core_off - KVM_REG_ARM_CORE_REG(spsr[0])) / CORE_SPSR_XX_NR_WORDS;
228215
TEST_ASSERT(idx < KVM_NR_SPSR, "%s: Unexpected spsr index: %lld", config_name(c), idx);
229-
return str_with_index("KVM_REG_ARM_CORE_REG(spsr[##])", idx);
216+
return strdup_printf("KVM_REG_ARM_CORE_REG(spsr[%lld])", idx);
230217
case KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]) ...
231218
KVM_REG_ARM_CORE_REG(fp_regs.vregs[31]):
232219
idx = (core_off - KVM_REG_ARM_CORE_REG(fp_regs.vregs[0])) / CORE_FPREGS_XX_NR_WORDS;
233220
TEST_ASSERT(idx < 32, "%s: Unexpected fp_regs.vregs index: %lld", config_name(c), idx);
234-
return str_with_index("KVM_REG_ARM_CORE_REG(fp_regs.vregs[##])", idx);
221+
return strdup_printf("KVM_REG_ARM_CORE_REG(fp_regs.vregs[%lld])", idx);
235222
case KVM_REG_ARM_CORE_REG(fp_regs.fpsr):
236223
return "KVM_REG_ARM_CORE_REG(fp_regs.fpsr)";
237224
case KVM_REG_ARM_CORE_REG(fp_regs.fpcr):
@@ -260,13 +247,13 @@ static const char *sve_id_to_str(struct vcpu_config *c, __u64 id)
260247
n = (id >> 5) & (KVM_ARM64_SVE_NUM_ZREGS - 1);
261248
TEST_ASSERT(id == KVM_REG_ARM64_SVE_ZREG(n, 0),
262249
"%s: Unexpected bits set in SVE ZREG id: 0x%llx", config_name(c), id);
263-
return str_with_index("KVM_REG_ARM64_SVE_ZREG(##, 0)", n);
250+
return strdup_printf("KVM_REG_ARM64_SVE_ZREG(%lld, 0)", n);
264251
case KVM_REG_ARM64_SVE_PREG_BASE ...
265252
KVM_REG_ARM64_SVE_PREG_BASE + (1ULL << 5) * KVM_ARM64_SVE_NUM_PREGS - 1:
266253
n = (id >> 5) & (KVM_ARM64_SVE_NUM_PREGS - 1);
267254
TEST_ASSERT(id == KVM_REG_ARM64_SVE_PREG(n, 0),
268255
"%s: Unexpected bits set in SVE PREG id: 0x%llx", config_name(c), id);
269-
return str_with_index("KVM_REG_ARM64_SVE_PREG(##, 0)", n);
256+
return strdup_printf("KVM_REG_ARM64_SVE_PREG(%lld, 0)", n);
270257
case KVM_REG_ARM64_SVE_FFR_BASE:
271258
TEST_ASSERT(id == KVM_REG_ARM64_SVE_FFR(0),
272259
"%s: Unexpected bits set in SVE FFR id: 0x%llx", config_name(c), id);

tools/testing/selftests/kvm/include/test_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,6 @@ static inline uint32_t atoi_non_negative(const char *name, const char *num_str)
186186
return num;
187187
}
188188

189+
char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
190+
189191
#endif /* SELFTEST_KVM_TEST_UTIL_H */

tools/testing/selftests/kvm/lib/test_util.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
* Copyright (C) 2020, Google LLC.
66
*/
77

8+
#define _GNU_SOURCE
9+
#include <stdio.h>
10+
#include <stdarg.h>
811
#include <assert.h>
912
#include <ctype.h>
1013
#include <limits.h>
@@ -377,3 +380,15 @@ int atoi_paranoid(const char *num_str)
377380

378381
return num;
379382
}
383+
384+
char *strdup_printf(const char *fmt, ...)
385+
{
386+
va_list ap;
387+
char *str;
388+
389+
va_start(ap, fmt);
390+
vasprintf(&str, fmt, ap);
391+
va_end(ap);
392+
393+
return str;
394+
}

0 commit comments

Comments
 (0)