Skip to content

Commit

Permalink
TypeHelpers: pre-callocate optionNames even if no names are provided (#…
Browse files Browse the repository at this point in the history
…444)

Prevents clients from reading junk values, as the optionNames array is optional.
  • Loading branch information
ericek111 authored Dec 21, 2024
1 parent 8c6cb7c commit 309335e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions include/SoapySDR/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ typedef struct
*/
SoapySDRRange range;

//! The size of the options set, or 0 when not used.
//! The size of the options and optionNames sets, or 0 when not used.
size_t numOptions;

/*!
Expand All @@ -102,7 +102,8 @@ typedef struct

/*!
* A discrete list of displayable names for the enumerated options (optional)
* When not specified, the option value itself can be used as a display name.
* When not specified, the respective entry in this list will be NULL, and
* the option key itself can be used as a display name instead.
*/
char **optionNames;

Expand Down
9 changes: 5 additions & 4 deletions lib/TypeHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ static inline char *toCString(const std::string &s)
return out;
}

static inline char **toStrArray(const std::vector<std::string> &strs, size_t *length)
static inline char **toStrArray(const std::vector<std::string> &strs, size_t *length, size_t minLength = 0)
{
auto out = callocArrayType<char *>(strs.size());
auto out = callocArrayType<char *>(std::max(minLength, strs.size()));
for (size_t i = 0; i < strs.size(); i++)
{
try
Expand Down Expand Up @@ -136,9 +136,10 @@ static inline SoapySDRArgInfo toArgInfo(const SoapySDR::ArgInfo &info)
out.units = toCString(info.units);
out.type = SoapySDRArgInfoType(info.type);
out.range = toRange(info.range);
out.optionNames = toStrArray(info.optionNames, &out.numOptions);
// do options after optionNames so correct numOptions is reported if no optionNames
out.options = toStrArray(info.options, &out.numOptions);
size_t namesLength = 0;
// will be calloc-ed to be at least as long as options to prevent clients from reading garbage
out.optionNames = toStrArray(info.optionNames, &namesLength, out.numOptions);
}
catch (const std::bad_alloc &)
{
Expand Down

0 comments on commit 309335e

Please sign in to comment.