From 556a8750a1d4c57d02bfe3a4caaffceaf816c783 Mon Sep 17 00:00:00 2001 From: initramfs Date: Sun, 3 Apr 2016 14:16:45 -0400 Subject: [PATCH] fix(video): usage of invalid file descriptors on error Fixes code for getDeviceModes() under vfl2 namespace where error numbers were being treated as valid file descriptors --- src/platform/camera/v4l2.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/platform/camera/v4l2.cpp b/src/platform/camera/v4l2.cpp index a442afad4..26c9a6d6b 100644 --- a/src/platform/camera/v4l2.cpp +++ b/src/platform/camera/v4l2.cpp @@ -56,28 +56,29 @@ static std::map createPixFmtToName() } const std::map pixFmtToName = createPixFmtToName(); -static int deviceOpen(QString devName) +static int deviceOpen(QString devName, int* error) { struct v4l2_capability cap; int fd; - int err; fd = open(devName.toStdString().c_str(), O_RDWR, 0); - if (fd < 0) - return errno; + if (fd < 0) { + *error = errno; + return fd; + } if (ioctl(fd, VIDIOC_QUERYCAP, &cap) < 0) { - err = errno; + *error = errno; goto fail; } if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { - err = ENODEV; + *error = ENODEV; goto fail; } if (!(cap.capabilities & V4L2_CAP_STREAMING)) { - err = ENOSYS; + *error = ENOSYS; goto fail; } @@ -85,7 +86,7 @@ static int deviceOpen(QString devName) fail: close(fd); - return err; + return -1; } static QVector getDeviceModeFramerates(int fd, unsigned w, unsigned h, uint32_t pixelFormat) @@ -120,8 +121,9 @@ QVector v4l2::getDeviceModes(QString devName) { QVector modes; - int fd = deviceOpen(devName); - if (fd < 0) + int error = 0; + int fd = deviceOpen(devName, &error); + if (fd < 0 || error != 0) return modes; v4l2_fmtdesc vfd{}; vfd.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;