aboutsummaryrefslogtreecommitdiffstats
path: root/meta-multimedia/recipes-multimedia/libcamera/libcamera/0002-options-Replace-use-of-VLAs-in-C.patch
blob: 473820653ea10bfb78dec985b34438ac97e80ec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
From 6e4736180fcaffdb06acf52fd3eb50ba5baa3d2a Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Wed, 31 Jan 2024 21:04:28 -0800
Subject: [PATCH] options: Replace use of VLAs in C++

Clang++ 18 is fussy about this with new warning checks.

   ../git/src/apps/common/options.cpp:882:20: error: variable length arrays in C++ are a Clang extension [-Werror,-Wvla-cxx-extension]
      882 |         char shortOptions[optionsMap_.size() * 3 + 2];
          |                           ^~~~~~~~~~~~~~~~~~~~~~~~~~

Therefore replace using VLAs with alloca and malloc/free

Upstream-Status: Submitted [https://lists.libcamera.org/pipermail/libcamera-devel/2024-February/040381.html]
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
 src/apps/common/options.cpp      | 12 ++++++++++--
 src/libcamera/ipc_unixsocket.cpp | 13 +++++++++----
 2 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/apps/common/options.cpp b/src/apps/common/options.cpp
index 4f7e8691..3656f3c1 100644
--- a/src/apps/common/options.cpp
+++ b/src/apps/common/options.cpp
@@ -879,8 +879,8 @@ OptionsParser::Options OptionsParser::parse(int argc, char **argv)
 	 * Allocate short and long options arrays large enough to contain all
 	 * options.
 	 */
-	char shortOptions[optionsMap_.size() * 3 + 2];
-	struct option longOptions[optionsMap_.size() + 1];
+	char *shortOptions = (char*)malloc(optionsMap_.size() * 3 + 2);
+	struct option *longOptions = (struct option*)malloc(sizeof(struct option) * (optionsMap_.size() + 1));
 	unsigned int ids = 0;
 	unsigned int idl = 0;
 
@@ -935,12 +935,16 @@ OptionsParser::Options OptionsParser::parse(int argc, char **argv)
 			std::cerr << argv[optind - 1] << std::endl;
 
 			usage();
+			free(shortOptions);
+			free(longOptions);
 			return options;
 		}
 
 		const Option &option = *optionsMap_[c];
 		if (!parseValue(option, optarg, &options)) {
 			usage();
+			free(shortOptions);
+			free(longOptions);
 			return options;
 		}
 	}
@@ -949,10 +953,14 @@ OptionsParser::Options OptionsParser::parse(int argc, char **argv)
 		std::cerr << "Invalid non-option argument '" << argv[optind]
 			  << "'" << std::endl;
 		usage();
+		free(shortOptions);
+		free(longOptions);
 		return options;
 	}
 
 	options.valid_ = true;
+	free(shortOptions);
+	free(longOptions);
 	return options;
 }
 
diff --git a/src/libcamera/ipc_unixsocket.cpp b/src/libcamera/ipc_unixsocket.cpp
index 1980d374..3bd861cb 100644
--- a/src/libcamera/ipc_unixsocket.cpp
+++ b/src/libcamera/ipc_unixsocket.cpp
@@ -8,6 +8,7 @@
 #include "libcamera/internal/ipc_unixsocket.h"
 
 #include <array>
+#include <cstdint>
 #include <poll.h>
 #include <string.h>
 #include <sys/socket.h>
@@ -247,8 +248,8 @@ int IPCUnixSocket::sendData(const void *buffer, size_t length,
 	iov[0].iov_base = const_cast<void *>(buffer);
 	iov[0].iov_len = length;
 
-	char buf[CMSG_SPACE(num * sizeof(uint32_t))];
-	memset(buf, 0, sizeof(buf));
+	char *buf = (char*)malloc(CMSG_SPACE(num * sizeof(uint32_t)));
+	memset((void*)buf, 0, sizeof(buf));
 
 	struct cmsghdr *cmsg = (struct cmsghdr *)buf;
 	cmsg->cmsg_len = CMSG_LEN(num * sizeof(uint32_t));
@@ -270,9 +271,11 @@ int IPCUnixSocket::sendData(const void *buffer, size_t length,
 		int ret = -errno;
 		LOG(IPCUnixSocket, Error)
 			<< "Failed to sendmsg: " << strerror(-ret);
+		free(buf);
 		return ret;
 	}
 
+	free(buf);
 	return 0;
 }
 
@@ -283,8 +286,8 @@ int IPCUnixSocket::recvData(void *buffer, size_t length,
 	iov[0].iov_base = buffer;
 	iov[0].iov_len = length;
 
-	char buf[CMSG_SPACE(num * sizeof(uint32_t))];
-	memset(buf, 0, sizeof(buf));
+	char *buf = (char*)malloc(CMSG_SPACE(num * sizeof(uint32_t)));
+	memset((void*)buf, 0, sizeof(buf));
 
 	struct cmsghdr *cmsg = (struct cmsghdr *)buf;
 	cmsg->cmsg_len = CMSG_LEN(num * sizeof(uint32_t));
@@ -305,12 +308,14 @@ int IPCUnixSocket::recvData(void *buffer, size_t length,
 		if (ret != -EAGAIN)
 			LOG(IPCUnixSocket, Error)
 				<< "Failed to recvmsg: " << strerror(-ret);
+		free(buf);
 		return ret;
 	}
 
 	if (fds)
 		memcpy(fds, CMSG_DATA(cmsg), num * sizeof(uint32_t));
 
+	free(buf);
 	return 0;
 }