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

Andrea Cervesato andrea.cervesato@suse.com
Mon Jan 13 15:05:44 CET 2025


Hi Cyril,

I guess this is affecting also TST_EXP_EQ_STR then.. I will need to fix 
that one as well.

Andrea

On 1/13/25 12:53, Cyril Hrubis wrote:
> 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


More information about the ltp mailing list