Replace all public `const char *` with `std::string_view`
diff --git a/src/gpioplus/event.cpp b/src/gpioplus/event.cpp
index d7e3716..88c5c47 100644
--- a/src/gpioplus/event.cpp
+++ b/src/gpioplus/event.cpp
@@ -25,14 +25,18 @@
static int build(const Chip& chip, uint32_t line_offset,
HandleFlags handle_flags, EventFlags event_flags,
- const char* consumer_label)
+ std::string_view consumer_label)
{
struct gpioevent_request req;
memset(&req, 0, sizeof(req));
req.lineoffset = line_offset;
req.handleflags = handle_flags.toInt();
req.eventflags = event_flags.toInt();
- strncpy(req.consumer_label, consumer_label, sizeof(req.consumer_label) - 1);
+ if (consumer_label.size() >= sizeof(req.consumer_label))
+ {
+ throw std::invalid_argument("consumer_label");
+ }
+ memcpy(req.consumer_label, consumer_label.data(), consumer_label.size());
int r = chip.getFd().getSys()->gpio_get_lineevent(*chip.getFd(), &req);
if (r < 0)
@@ -45,7 +49,7 @@
}
Event::Event(const Chip& chip, uint32_t line_offset, HandleFlags handle_flags,
- EventFlags event_flags, const char* consumer_label) :
+ EventFlags event_flags, std::string_view consumer_label) :
fd(build(chip, line_offset, handle_flags, event_flags, consumer_label),
std::false_type(), chip.getFd().getSys())
{
diff --git a/src/gpioplus/event.hpp b/src/gpioplus/event.hpp
index 06d0a3a..7e40cf0 100644
--- a/src/gpioplus/event.hpp
+++ b/src/gpioplus/event.hpp
@@ -4,6 +4,7 @@
#include <gpioplus/handle.hpp>
#include <gpioplus/internal/fd.hpp>
#include <optional>
+#include <string_view>
namespace gpioplus
{
@@ -20,7 +21,7 @@
{
public:
Event(const Chip& chip, uint32_t line_offset, HandleFlags handle_flags,
- EventFlags event_flags, const char* consumer_label);
+ EventFlags event_flags, std::string_view consumer_label);
const internal::Fd& getFd() const;
diff --git a/src/gpioplus/handle.cpp b/src/gpioplus/handle.cpp
index bcf581d..a001ed4 100644
--- a/src/gpioplus/handle.cpp
+++ b/src/gpioplus/handle.cpp
@@ -41,7 +41,7 @@
}
static int build(const Chip& chip, const std::vector<Handle::Line>& lines,
- HandleFlags flags, const char* consumer_label)
+ HandleFlags flags, std::string_view consumer_label)
{
if (lines.size() > GPIOHANDLES_MAX)
{
@@ -56,7 +56,11 @@
req.default_values[i] = lines[i].default_value;
}
req.flags = flags.toInt();
- strncpy(req.consumer_label, consumer_label, sizeof(req.consumer_label) - 1);
+ if (consumer_label.size() >= sizeof(req.consumer_label))
+ {
+ throw std::invalid_argument("consumer_label");
+ }
+ memcpy(req.consumer_label, consumer_label.data(), consumer_label.size());
req.lines = lines.size();
int r = chip.getFd().getSys()->gpio_get_linehandle(*chip.getFd(), &req);
@@ -70,7 +74,7 @@
}
Handle::Handle(const Chip& chip, const std::vector<Line>& lines,
- HandleFlags flags, const char* consumer_label) :
+ HandleFlags flags, std::string_view consumer_label) :
fd(build(chip, lines, flags, consumer_label), std::false_type(),
chip.getFd().getSys()),
nlines(lines.size())
diff --git a/src/gpioplus/handle.hpp b/src/gpioplus/handle.hpp
index e31483b..8bdf4d5 100644
--- a/src/gpioplus/handle.hpp
+++ b/src/gpioplus/handle.hpp
@@ -2,6 +2,7 @@
#include <cstdint>
#include <gpioplus/chip.hpp>
#include <gpioplus/internal/fd.hpp>
+#include <string_view>
#include <vector>
namespace gpioplus
@@ -29,7 +30,7 @@
uint8_t default_value;
};
Handle(const Chip& chip, const std::vector<Line>& lines, HandleFlags flags,
- const char* consumer_label);
+ std::string_view consumer_label);
const internal::Fd& getFd() const;
diff --git a/test/event.cpp b/test/event.cpp
index fa2337e..ed627e8 100644
--- a/test/event.cpp
+++ b/test/event.cpp
@@ -83,6 +83,15 @@
EXPECT_CALL(mock, close(event_fd)).WillOnce(Return(0));
}
+TEST_F(EventTest, ConstructLabelTooLong)
+{
+ const size_t large_size = sizeof(
+ reinterpret_cast<struct gpioevent_request*>(NULL)->consumer_label);
+ EXPECT_THROW(Event(*chip, 0, HandleFlags(), EventFlags(),
+ std::string(large_size, '1')),
+ std::invalid_argument);
+}
+
TEST_F(EventTest, ConstructFailure)
{
const uint32_t line_offset = 3;
diff --git a/test/handle.cpp b/test/handle.cpp
index 08a66db..6370c30 100644
--- a/test/handle.cpp
+++ b/test/handle.cpp
@@ -111,6 +111,14 @@
std::runtime_error);
}
+TEST_F(HandleTest, ConstructLabelTooLong)
+{
+ const size_t large_size = sizeof(
+ reinterpret_cast<struct gpiohandle_request*>(NULL)->consumer_label);
+ EXPECT_THROW(Handle(*chip, {}, HandleFlags(), std::string(large_size, '1')),
+ std::invalid_argument);
+}
+
TEST_F(HandleTest, ConstructError)
{
const std::string label{"error"};