[LTP] [PATCH v3 09/12] Add TST_EXP_EQ_STRN macro

Cyril Hrubis chrubis@suse.cz
Mon Jan 13 12:53:53 CET 2025


Hi!
> +#define TST_EXP_EQ_STRN(STR_A, STR_B, LEN) do {                                \
> +	TST_PASS = strncmp(STR_A, STR_B, LEN) == 0;                            \
> +                                                                               \
> +	if (TST_PASS) {                                                        \
> +		tst_res_(__FILE__, __LINE__, TPASS,                            \
> +			"%s == %s (%s)",                                       \
> +			#STR_A, #STR_B, STR_B);                                \
> +	} else {                                                               \
> +		tst_res_(__FILE__, __LINE__, TFAIL,                            \
> +			"%s (%s) != %s (%s)",                                  \
> +			#STR_A, STR_A, #STR_B, STR_B);                         \

Passing these strings to printf-like function is not safe at all, since
they are possibly not nul terminated. If we realy wanted to print them
we would have to copy them and nul terminated them.

e.g.

	char str_a_cpy[LEN+1], str_b_cpy[LEN+1];

	strncpy(str_a_cpy, STR_A, LEN);
	str_a_cpy[LEN] = 0;
	strncpy(str_b_cpy, STR_B, LEN);
	str_b_cpy[LEN] = 0;

	...

		tst_res_(...., str_b_cpy, ...);

> +	}                                                                      \
> +} while (0)
> +
>  #endif	/* TST_TEST_MACROS_H__ */
> 
> -- 
> 2.43.0
> 
> 
> -- 
> Mailing list info: https://lists.linux.it/listinfo/ltp

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list