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

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 <ajones@ventanamicro.com>
Signed-off-by: Haibo Xu <haibo1.xu@intel.com>
Signed-off-by: Anup Patel <anup@brainfault.org>

authored by

Andrew Jones and committed by
Anup Patel
dfaf20af 630b4cee

+22 -18
+5 -18
tools/testing/selftests/kvm/aarch64/get-reg-list.c
··· 180 180 return true; 181 181 } 182 182 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 - 196 183 #define REG_MASK (KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_COPROC_MASK) 197 184 198 185 #define CORE_REGS_XX_NR_WORDS 2 ··· 198 211 KVM_REG_ARM_CORE_REG(regs.regs[30]): 199 212 idx = (core_off - KVM_REG_ARM_CORE_REG(regs.regs[0])) / CORE_REGS_XX_NR_WORDS; 200 213 TEST_ASSERT(idx < 31, "%s: Unexpected regs.regs index: %lld", config_name(c), idx); 201 - return str_with_index("KVM_REG_ARM_CORE_REG(regs.regs[##])", idx); 214 + return strdup_printf("KVM_REG_ARM_CORE_REG(regs.regs[%lld])", idx); 202 215 case KVM_REG_ARM_CORE_REG(regs.sp): 203 216 return "KVM_REG_ARM_CORE_REG(regs.sp)"; 204 217 case KVM_REG_ARM_CORE_REG(regs.pc): ··· 213 226 KVM_REG_ARM_CORE_REG(spsr[KVM_NR_SPSR - 1]): 214 227 idx = (core_off - KVM_REG_ARM_CORE_REG(spsr[0])) / CORE_SPSR_XX_NR_WORDS; 215 228 TEST_ASSERT(idx < KVM_NR_SPSR, "%s: Unexpected spsr index: %lld", config_name(c), idx); 216 - return str_with_index("KVM_REG_ARM_CORE_REG(spsr[##])", idx); 229 + return strdup_printf("KVM_REG_ARM_CORE_REG(spsr[%lld])", idx); 217 230 case KVM_REG_ARM_CORE_REG(fp_regs.vregs[0]) ... 218 231 KVM_REG_ARM_CORE_REG(fp_regs.vregs[31]): 219 232 idx = (core_off - KVM_REG_ARM_CORE_REG(fp_regs.vregs[0])) / CORE_FPREGS_XX_NR_WORDS; 220 233 TEST_ASSERT(idx < 32, "%s: Unexpected fp_regs.vregs index: %lld", config_name(c), idx); 221 - return str_with_index("KVM_REG_ARM_CORE_REG(fp_regs.vregs[##])", idx); 234 + return strdup_printf("KVM_REG_ARM_CORE_REG(fp_regs.vregs[%lld])", idx); 222 235 case KVM_REG_ARM_CORE_REG(fp_regs.fpsr): 223 236 return "KVM_REG_ARM_CORE_REG(fp_regs.fpsr)"; 224 237 case KVM_REG_ARM_CORE_REG(fp_regs.fpcr): ··· 247 260 n = (id >> 5) & (KVM_ARM64_SVE_NUM_ZREGS - 1); 248 261 TEST_ASSERT(id == KVM_REG_ARM64_SVE_ZREG(n, 0), 249 262 "%s: Unexpected bits set in SVE ZREG id: 0x%llx", config_name(c), id); 250 - return str_with_index("KVM_REG_ARM64_SVE_ZREG(##, 0)", n); 263 + return strdup_printf("KVM_REG_ARM64_SVE_ZREG(%lld, 0)", n); 251 264 case KVM_REG_ARM64_SVE_PREG_BASE ... 252 265 KVM_REG_ARM64_SVE_PREG_BASE + (1ULL << 5) * KVM_ARM64_SVE_NUM_PREGS - 1: 253 266 n = (id >> 5) & (KVM_ARM64_SVE_NUM_PREGS - 1); 254 267 TEST_ASSERT(id == KVM_REG_ARM64_SVE_PREG(n, 0), 255 268 "%s: Unexpected bits set in SVE PREG id: 0x%llx", config_name(c), id); 256 - return str_with_index("KVM_REG_ARM64_SVE_PREG(##, 0)", n); 269 + return strdup_printf("KVM_REG_ARM64_SVE_PREG(%lld, 0)", n); 257 270 case KVM_REG_ARM64_SVE_FFR_BASE: 258 271 TEST_ASSERT(id == KVM_REG_ARM64_SVE_FFR(0), 259 272 "%s: Unexpected bits set in SVE FFR id: 0x%llx", config_name(c), id);
+2
tools/testing/selftests/kvm/include/test_util.h
··· 186 186 return num; 187 187 } 188 188 189 + char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1))); 190 + 189 191 #endif /* SELFTEST_KVM_TEST_UTIL_H */
+15
tools/testing/selftests/kvm/lib/test_util.c
··· 5 5 * Copyright (C) 2020, Google LLC. 6 6 */ 7 7 8 + #define _GNU_SOURCE 9 + #include <stdio.h> 10 + #include <stdarg.h> 8 11 #include <assert.h> 9 12 #include <ctype.h> 10 13 #include <limits.h> ··· 379 376 "%ld not in range of [%d, %d]", num, INT_MIN, INT_MAX); 380 377 381 378 return num; 379 + } 380 + 381 + char *strdup_printf(const char *fmt, ...) 382 + { 383 + va_list ap; 384 + char *str; 385 + 386 + va_start(ap, fmt); 387 + vasprintf(&str, fmt, ap); 388 + va_end(ap); 389 + 390 + return str; 382 391 }