[LTP] [PATCH 2/6] fs: rewrite stream01 test using new API

Cyril Hrubis chrubis@suse.cz
Thu Feb 19 14:05:40 CET 2026


Hi!
> -char *TCID = "stream01";
> -int TST_TOTAL = 1;
> -int local_flag;
> +static void read_file(const char *file)
> +{
> +	char buf[2];
> +	FILE *stream;
>  
> -#define PASSED 1
> -#define FAILED 0
> +	memset(buf, 0, sizeof(buf));
>  
> -/* XXX: add setup and cleanup. */
> +	stream = SAFE_FOPEN(file, "r");
> +	SAFE_FREAD(buf, 1, 1, stream);

Here we read at most 1 character from the stream.

> +	SAFE_FCLOSE(stream);
>  
> -char progname[] = "stream01()";
> -char tempfile1[40] = "";
> -char tempfile2[40] = "";
> +	TST_EXP_EXPR((buf[0] == 'a') && (buf[1] == 0),
                                         ^
					 Hence this is always true
> +		"%s file contains the correct data", file);

If we wanted to assert that the file has a single characted written into
it we need to let the fread() read the whole buffer:

	SAFE_FREAD(buf, 2, 1, stream);

With that there is at least chance that the buf[1] may be overwritten
with some data.

Or we can check the size returned from fread(), if it was 1 there was a
single characted in the file. But again, we have to pass buffer that is
at least 2 bytes long.

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list