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

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

Trim trailing NIL character when fetching string attribute

Also, fix one instance of non-inclusive language that slipped through.

Bug: 162032964
Bug: 162745675
Test: custom implementation to watch for NEWLINK/DELLINK messages
Change-Id: I8fe05731fd9d598ec3aec1452aa04ac764e9c7e7
parent 4b8f50c1
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -49,7 +49,11 @@ Attributes Attributes::parse(Buffer<nlattr> buf) {
template <>
std::string Attributes::parse(Buffer<nlattr> buf) {
    const auto rawString = buf.data<char>().getRaw();
    return std::string(rawString.ptr(), rawString.len());
    std::string str(rawString.ptr(), rawString.len());

    str.erase(std::find(str.begin(), str.end(), '\0'), str.end());

    return str;
}

template <typename T>
+1 −3
Original line number Diff line number Diff line
@@ -32,9 +32,7 @@ unsigned int nametoindex(const std::string& ifname) {
    return 0;
}

std::string sanitize(std::string str) {
    str.erase(std::find(str.begin(), str.end(), '\0'), str.end());

std::string printableOnly(std::string str) {
    const auto isInvalid = [](char c) { return !isprint(c); };
    std::replace_if(str.begin(), str.end(), isInvalid, '?');

+6 −3
Original line number Diff line number Diff line
@@ -37,11 +37,14 @@ namespace android::nl {
unsigned int nametoindex(const std::string& ifname);

/**
 * Sanitize a string of unknown contents.
 * Filter a string against non-printable characters.
 *
 * Trims the string to the first '\0' character and replaces all non-printable characters with '?'.
 * Replaces all non-printable characters with '?'.
 *
 * \param str String to filter.
 * \return Filtered string.
 */
std::string sanitize(std::string str);
std::string printableOnly(std::string str);

/**
 * Calculates a (optionally running) CRC16 checksum.
+1 −1
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ static void toStream(std::stringstream& ss, const Buffer<nlattr> attr,
        }
        case DataType::String: {
            const auto str = attr.data<char>().getRaw();
            ss << '"' << sanitize({str.ptr(), str.len()}) << '"';
            ss << '"' << printableOnly({str.ptr(), str.len()}) << '"';
            break;
        }
        case DataType::Uint: