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

Commit df93ad65 authored by Harry Cutts's avatar Harry Cutts
Browse files

InputDevice: replace strncpy and snprintf in dump

The explicit addition of a null terminator afterwards _should_ have made
the strncpy usage safe, but even so it's harder to understand and adds
an additional copy.

Test: comment out most of the AXES_SEQUENCES list in
      libs/input/InputEventLabels.cpp and check that both labels and
      numbers are shown correctly in `adb shell dumpsys input`
Bug: 245989146
Flag: EXEMPT refactor
Change-Id: I5a00ec35f8a035b0a29e4f08d8d1c89743ad00d1
parent 9ff64f6a
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include "InputDevice.h"

#include <algorithm>
#include <string>

#include <android/sysprop/InputProperties.sysprop.h>
#include <ftl/flags.h>
@@ -147,20 +148,16 @@ void InputDevice::dump(std::string& dump, const std::string& eventHubDevStr) {
    const std::vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
    if (!ranges.empty()) {
        dump += INDENT2 "Motion Ranges:\n";
        for (size_t i = 0; i < ranges.size(); i++) {
            const InputDeviceInfo::MotionRange& range = ranges[i];
            const char* label = InputEventLookup::getAxisLabel(range.axis);
            char name[32];
            if (label) {
                strncpy(name, label, sizeof(name));
                name[sizeof(name) - 1] = '\0';
        for (const auto& range : ranges) {
            dump += INDENT3;
            if (const char* label = InputEventLookup::getAxisLabel(range.axis); label != nullptr) {
                dump += label;
            } else {
                snprintf(name, sizeof(name), "%d", range.axis);
                dump += std::to_string(range.axis);
            }
            dump += StringPrintf(INDENT3
                                 "%s: source=%s, "
            dump += StringPrintf(": source=%s, "
                                 "min=%0.3f, max=%0.3f, flat=%0.3f, fuzz=%0.3f, resolution=%0.3f\n",
                                 name, inputEventSourceToString(range.source).c_str(), range.min,
                                 inputEventSourceToString(range.source).c_str(), range.min,
                                 range.max, range.flat, range.fuzz, range.resolution);
        }
    }