This is an automated email from the ASF dual-hosted git repository. jiuzhudong pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit f16a45a854fa5cdaab0cfe77921d73bf301caf47 Author: wangzhi16 <[email protected]> AuthorDate: Wed May 28 22:34:35 2025 +0800 examples/pipe: bugfix testcase of fifo There are three bug in fifo testcase. 1. line 211 and line 295 should be nbytes not ret. 2. coverity report error: CID 1667262: (#4 of 11): Double close (USE_AFTER_FREE) 11. double_close: Calling close(int) will close the handle fd that has already been closed. [Note: The source code implementation of this function has been overwritten by the built-in model.] Double close will ocurr when line 234 is true and goto line 341, close again! Now, Using the previous code logic, Use a tid and an errout_with_null_thread to avoid uninitialized problems. 3. since line 295 is used ret not nbytes, error not expose before. When the end of read and write are opened, end of write will write successfully even if end of read not read. Signed-off-by: wangzhi16 <[email protected]> --- examples/pipe/interlock_test.c | 55 +++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/examples/pipe/interlock_test.c b/examples/pipe/interlock_test.c index 6be9b6ec4..cb2985800 100644 --- a/examples/pipe/interlock_test.c +++ b/examples/pipe/interlock_test.c @@ -150,8 +150,7 @@ static void *null_writer(pthread_addr_t pvarg) int interlock_test(void) { - pthread_t readerid; - pthread_t writerid; + pthread_t tid; void *value; char data[16]; ssize_t nbytes; @@ -171,7 +170,7 @@ int interlock_test(void) /* Start the null_writer thread */ printf("interlock_test: Starting null_writer thread\n"); - ret = pthread_create(&writerid, NULL, null_writer, NULL); + ret = pthread_create(&tid, NULL, null_writer, NULL); if (ret != 0) { fprintf(stderr, \ @@ -194,7 +193,7 @@ int interlock_test(void) "errno=%d\n", FIFO_PATH2, errno); ret = 3; - goto errout_with_null_writer_thread; + goto errout_with_null_thread; } /* Attempt to read one byte from the FIFO. This should return end-of-file @@ -208,15 +207,15 @@ int interlock_test(void) fprintf(stderr, \ "interlock_test: read failed, errno=%d\n", errno); ret = 4; - goto errout_with_null_writer_thread; + goto errout_with_file; } - else if (ret != 0) + else if (nbytes != 0) { fprintf(stderr, \ "interlock_test: Read %ld bytes of data -- aborting: %d\n", (long)nbytes, errno); ret = 5; - goto errout_with_null_writer_thread; + goto errout_with_file; } printf("interlock_test: read returned\n"); @@ -234,7 +233,7 @@ int interlock_test(void) /* Wait for null_writer thread to complete */ printf("interlock_test: Waiting for null_writer thread\n"); - ret = pthread_join(writerid, &value); + ret = pthread_join(tid, &value); if (ret != 0) { fprintf(stderr, \ @@ -255,7 +254,7 @@ int interlock_test(void) /* Start the null_reader thread */ printf("interlock_test: Starting null_reader thread\n"); - ret = pthread_create(&readerid, NULL, null_reader, NULL); + ret = pthread_create(&tid, NULL, null_reader, NULL); if (ret != 0) { fprintf(stderr, \ @@ -278,29 +277,26 @@ int interlock_test(void) "errno=%d\n", FIFO_PATH2, errno); ret = 9; - goto errout_with_null_reader_thread; + goto errout_with_null_thread; } - /* Attempt to write one byte from the FIFO. This should return 0 bytes - * written because the null_reader closes the FIFO. + /* Attempt to write one byte from the FIFO. This should return n bytes + * written since the null_reader has opened the FIFO. */ printf("interlock_test: Writing to %s\n", FIFO_PATH2); nbytes = write(fd, data, 16); - if (nbytes < 0) + if (nbytes <= 0) { fprintf(stderr, \ "interlock_test: write failed, errno=%d\n", errno); ret = 10; - goto errout_with_null_reader_thread; + goto errout_with_file; } - else if (ret != 0) + else { fprintf(stderr, \ - "interlock_test: Wrote %ld bytes of data -- aborting: %d\n", - (long)nbytes, errno); - ret = 11; - goto errout_with_null_reader_thread; + "interlock_test: Wrote %ld bytes of data\n", (long)nbytes); } printf("interlock_test: write returned\n"); @@ -318,7 +314,7 @@ int interlock_test(void) /* Wait for null_reader thread to complete */ printf("interlock_test: Waiting for null_reader thread\n"); - ret = pthread_join(readerid, &value); + ret = pthread_join(tid, &value); if (ret != 0) { fprintf(stderr, \ @@ -339,20 +335,19 @@ int interlock_test(void) ret = 0; goto errout_with_fifo; -errout_with_null_reader_thread: - pthread_detach(readerid); - pthread_cancel(readerid); +errout_with_file: + if (close(fd) != 0) + { + fprintf(stderr, "interlock_test: close failed: %d\n", errno); + } -errout_with_null_writer_thread: - pthread_detach(writerid); - pthread_cancel(writerid); +errout_with_null_thread: + pthread_detach(tid); + pthread_cancel(tid); errout_with_fifo: - if (fd != -1 && close(fd) != 0) - { - fprintf(stderr, "interlock_test: close failed: %d\n", errno); - } + /* Close the file */ ret = remove(FIFO_PATH2); if (ret != 0)
