[PATCH v2] Add new tests for fopen
Florian Weimer
fweimer@redhat.com
Sun Feb 2 13:47:21 GMT 2025
* Martin Coufal:
> +static void *
> +threadReadRoutine (void *argv)
> +{
> + struct thread_data *my_data;
> + unsigned char read_buffer;
> + int ret = 0;
> + my_data = (struct thread_data *) argv;
> + /* Wait for all threads to be ready to read. */
> + xpthread_barrier_wait (&barrier);
> +
> + ret =
> + fread (&read_buffer, sizeof (char), sizeof (read_buffer), my_data->fd);
> + if (feof (my_data->fd) != 0)
> + {
> + clearerr (my_data->fd);
> + my_data->eof = true;
> + }
> + else
> + {
> + TEST_COMPARE (ret, 1);
> + /* Save the read value. */
> + my_data->value = read_buffer;
> + }
> + for (int i = 0; i < NUM_THREADS; i++)
> + {
> + if (thread_data_array[i].eof)
> + {
> + /* EOF was read. */
> + present_values[NUM_THREADS - 1] = true;
> + eof_cnt++;
> + }
> + else
> + {
> + /* The same value shouldn't be read twice. */
> + TEST_VERIFY (!present_values[thread_data_array[i].value]);
> + present_values[thread_data_array[i].value] = true;
> + }
> + }
> + /* EOF is read exactly once. */
> + TEST_COMPARE (eof_cnt, 1);
This test has a race condition: more than one thread can report
end-of-file because the check for this condition is separate from the
fread call. It is possible that all threads return from their fread
calls before feof is called on multiple threads. This test bug is quite
visible on larger AArch64 systems, for example.
Thanks,
Florian
More information about the Libc-alpha
mailing list