Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b141ef6f authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk
Browse files

libnl++: fix std::string_view handling

This is a partial revert of If51380cab95b2ae725673301b429b9e9889c5b0a

Changes:
- combine add() and addBuffer - how both skip the trailing '\0'
- assert that add<A>() is only used for POD
- implement add overload for std::string_view

Bug: 372814636
Test: m
Change-Id: I08d8825636a3a908e0ebd3bd312580a27505c330
parent 5db6311d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -80,7 +80,7 @@ bool setBitrate(std::string_view ifname, uint32_t bitrate) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.addBuffer(IFLA_INFO_KIND, "can");
        req.add(IFLA_INFO_KIND, "can");
        {
            auto infodata = req.addNested(IFLA_INFO_DATA);
            /* For CAN FD, it would require to add IFLA_CAN_DATA_BITTIMING
+1 −1
Original line number Diff line number Diff line
@@ -141,7 +141,7 @@ bool add(std::string_view dev, std::string_view type) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.addBuffer(IFLA_INFO_KIND, type);
        req.add(IFLA_INFO_KIND, type);
    }

    nl::Socket sock(NETLINK_ROUTE);
+1 −1
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ bool add(std::string_view eth, std::string_view vlan, uint16_t id) {

    {
        auto linkinfo = req.addNested(IFLA_LINKINFO);
        req.addBuffer(IFLA_INFO_KIND, "vlan");
        req.add(IFLA_INFO_KIND, "vlan");

        {
            auto linkinfo = req.addNested(IFLA_INFO_DATA);
+8 −11
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <linux/netlink.h>

#include <string>
#include <type_traits>

namespace android::nl {

@@ -109,25 +110,21 @@ class MessageFactory : private MessageFactoryBase {
     */
    template <class A>
    void add(nlattrtype_t type, const A& attr) {
        addInternal(type, &attr, sizeof(attr));
        static_assert(std::is_pod_v<A>, "POD type required");
        add(type, &attr, sizeof(attr));
    }

    // It will always send the last null character, otherwise use addBuffer
    // variant instead
    template <>
    void add(nlattrtype_t type, const std::string& s) {
        addInternal(type, s.c_str(), s.size() + 1);
        add(type, s.c_str(), s.size());
    }

    void addBuffer(nlattrtype_t type, const std::string_view& s) {
        addInternal(type, s.data(), s.size());
    }
    void add(nlattrtype_t type, std::string_view s) { add(type, s.data(), s.size()); }

    /** Guard class to frame nested attributes. \see addNested(nlattrtype_t). */
    class [[nodiscard]] NestedGuard {
      public:
        NestedGuard(MessageFactory& req, nlattrtype_t type)
            : mReq(req), mAttr(req.addInternal(type)) {}
        NestedGuard(MessageFactory& req, nlattrtype_t type) : mReq(req), mAttr(req.add(type)) {}
        ~NestedGuard() { closeNested(&mReq.mMessage.header, mAttr); }

      private:
@@ -148,7 +145,7 @@ class MessageFactory : private MessageFactoryBase {
     *    MessageFactory<ifinfomsg> req(RTM_NEWLINK, NLM_F_REQUEST);
     *    {
     *        auto linkinfo = req.addNested(IFLA_LINKINFO);
     *        req.addBuffer(IFLA_INFO_KIND, "can");
     *        req.add(IFLA_INFO_KIND, "can");
     *        {
     *            auto infodata = req.addNested(IFLA_INFO_DATA);
     *            req.add(IFLA_CAN_BITTIMING, bitTimingStruct);
@@ -164,7 +161,7 @@ class MessageFactory : private MessageFactoryBase {
    Message mMessage = {};
    bool mIsGood = true;

    nlattr* addInternal(nlattrtype_t type, const void* data = nullptr, size_t len = 0) {
    nlattr* add(nlattrtype_t type, const void* data = nullptr, size_t len = 0) {
        if (!mIsGood) return nullptr;
        auto attr = MessageFactoryBase::add(&mMessage.header, sizeof(mMessage), type, data, len);
        if (attr == nullptr) mIsGood = false;