[PATCH] libv4lconvert: Don't ignore return value of ftruncate()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The ftruncate() function is declared with the warn_unused_result
attribute, which generates a warning when compiling libv4lconvert:

[60/340] Compiling C object lib/libv4lconvert/libv4lconvert.so.0.0.0.p/control_libv4lcontrol.c.o
../lib/libv4lconvert/control/libv4lcontrol.c: In function ‘v4lcontrol_create’:
../lib/libv4lconvert/control/libv4lcontrol.c:728:17: warning: ignoring return value of ‘ftruncate’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  728 |                 ftruncate(shm_fd, V4LCONTROL_SHM_SIZE);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fix it by checking the return value and falling back to malloc-ed memory
for controls, as done when mmap() fails.

While at it, fix a typo in a comment, and drop an unneeded "error" word
from an error message.

Signed-off-by: Laurent Pinchart <laurent.pinchart@xxxxxxxxxxxxxxxx>
---
 lib/libv4lconvert/control/libv4lcontrol.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/lib/libv4lconvert/control/libv4lcontrol.c b/lib/libv4lconvert/control/libv4lcontrol.c
index 7296de1d0fcb..676719372bb7 100644
--- a/lib/libv4lconvert/control/libv4lcontrol.c
+++ b/lib/libv4lconvert/control/libv4lcontrol.c
@@ -725,16 +725,21 @@ struct v4lcontrol_data *v4lcontrol_create(int fd, void *dev_ops_priv,
 
 	if (shm_fd >= 0) {
 		/* Set the shared memory size */
-		ftruncate(shm_fd, V4LCONTROL_SHM_SIZE);
+		int ret = ftruncate(shm_fd, V4LCONTROL_SHM_SIZE);
+		if (ret) {
+			perror("libv4lcontrol: shm ftruncate failed");
+			close(shm_fd);
+		} else {
+			/* Retrieve a pointer to the shm object */
+			data->shm_values = mmap(NULL, V4LCONTROL_SHM_SIZE,
+						PROT_READ | PROT_WRITE,
+						MAP_SHARED, shm_fd, 0);
+			close(shm_fd);
 
-		/* Retreive a pointer to the shm object */
-		data->shm_values = mmap(NULL, V4LCONTROL_SHM_SIZE, (PROT_READ | PROT_WRITE),
-				MAP_SHARED, shm_fd, 0);
-		close(shm_fd);
-
-		if (data->shm_values == MAP_FAILED) {
-			perror("libv4lcontrol: error shm mmap failed");
-			data->shm_values = NULL;
+			if (data->shm_values == MAP_FAILED) {
+				perror("libv4lcontrol: shm mmap failed");
+				data->shm_values = NULL;
+			}
 		}
 	} else
 		perror("libv4lcontrol: error creating shm segment failed");
-- 
Regards,

Laurent Pinchart




[Index of Archives]     [Linux Input]     [Video for Linux]     [Gstreamer Embedded]     [Mplayer Users]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]

  Powered by Linux