apps: cam: capture_script: Simplify bool array parsing

`std::vector<bool>` is a specialization that implements a dynamic
bit vector, therefore it is not suitable to provide storage for
an array of `bool`. Hence a statically sized array is used when
parsing an array of boolean values.

Instead, use the array overload of `std::make_unique` since the
size is known beforehand.

Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo.mondi@ideasonboard.com>
This commit is contained in:
Barnabás Pőcze 2025-04-16 21:44:25 +02:00
parent 83543f08d5
commit 3e4de5f54e

View file

@ -8,6 +8,7 @@
#include "capture_script.h" #include "capture_script.h"
#include <iostream> #include <iostream>
#include <memory>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -521,45 +522,22 @@ ControlValue CaptureScript::parseArrayControl(const ControlId *id,
case ControlTypeNone: case ControlTypeNone:
break; break;
case ControlTypeBool: { case ControlTypeBool: {
/* auto values = std::make_unique<bool[]>(repr.size());
* This is unpleasant, but we cannot use an std::vector<> as its
* boolean type overload does not allow to access the raw data,
* as boolean values are stored in a bitmask for efficiency.
*
* As we need a contiguous memory region to wrap in a Span<>,
* use an array instead but be strict about not overflowing it
* by limiting the number of controls we can store.
*
* Be loud but do not fail, as the issue would present at
* runtime and it's not fatal.
*/
static constexpr unsigned int kMaxNumBooleanControls = 1024;
std::array<bool, kMaxNumBooleanControls> values;
unsigned int idx = 0;
for (const std::string &s : repr) { for (std::size_t i = 0; i < repr.size(); i++) {
bool val; const auto &s = repr[i];
if (s == "true") { if (s == "true") {
val = true; values[i] = true;
} else if (s == "false") { } else if (s == "false") {
val = false; values[i] = false;
} else { } else {
unpackFailure(id, s); unpackFailure(id, s);
return value; return value;
} }
if (idx == kMaxNumBooleanControls) {
std::cerr << "Cannot parse more than "
<< kMaxNumBooleanControls
<< " boolean controls" << std::endl;
break;
} }
values[idx++] = val; value = Span<bool>(values.get(), repr.size());
}
value = Span<bool>(values.data(), idx);
break; break;
} }
case ControlTypeByte: { case ControlTypeByte: {