From ecd0066e806741c39e72f7f274d5626dbe6ef1f3 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Mon, 6 Mar 2023 17:19:29 +0100 Subject: [PATCH] os: remove use of format strings Issue #2064 --- repos/os/src/app/usb_report_filter/main.cc | 49 ++++++++------------- repos/os/src/drivers/acpi/acpi.cc | 4 +- repos/os/src/drivers/virtdev_rom/main.cc | 6 +-- repos/os/src/lib/vfs/file_system_factory.cc | 6 +-- repos/os/src/lib/vfs/rtc_file_system.h | 30 +++++++++++-- repos/os/src/server/terminal_log/main.cc | 13 ++++-- repos/os/src/test/clipboard/main.cc | 7 +-- repos/os/src/test/vfs_stress/main.cc | 24 +++++----- 8 files changed, 71 insertions(+), 68 deletions(-) diff --git a/repos/os/src/app/usb_report_filter/main.cc b/repos/os/src/app/usb_report_filter/main.cc index 0b3c4cf502..40a4890a47 100644 --- a/repos/os/src/app/usb_report_filter/main.cc +++ b/repos/os/src/app/usb_report_filter/main.cc @@ -29,7 +29,6 @@ namespace Usb_filter { using Genode::Xml_node; using Genode::Xml_generator; using Genode::Attached_rom_dataspace; - using Genode::snprintf; using Genode::error; using Genode::log; using Genode::warning; @@ -120,29 +119,30 @@ class Usb_filter::Device_registry (vendor == entry.vendor && product == entry.product); } + static void _gen_dev_attributes(Xml_generator &xml, Xml_node const &node) + { + auto copy_attr = [&] (auto name) + { + using Value = Genode::String<32>; + xml.attribute(name, node.attribute_value(name, Value())); + }; + + copy_attr("vendor_id"); + copy_attr("product_id"); + copy_attr("bus"); + copy_attr("dev"); + } + static void _gen_policy_entry(Xml_generator &xml, Xml_node &node, Entry const &, char const *label) { xml.node("policy", [&] { - char buf[MAX_LABEL_LEN + 16]; - unsigned const bus = _get_value(node, "bus"); unsigned const dev = _get_value(node, "dev"); - snprintf(buf, sizeof(buf), "%s -> usb-%d-%d", label, bus, dev); - xml.attribute("label", buf); + xml.attribute("label", Label(label, " -> usb-", bus, "-", dev)); - snprintf(buf, sizeof(buf), "0x%4x", _get_value(node, "vendor_id")); - xml.attribute("vendor_id", buf); - - snprintf(buf, sizeof(buf), "0x%4x", _get_value(node, "product_id")); - xml.attribute("product_id", buf); - - snprintf(buf, sizeof(buf), "0x%4x", bus); - xml.attribute("bus", buf); - - snprintf(buf, sizeof(buf), "0x%4x", dev); - xml.attribute("dev", buf); + _gen_dev_attributes(xml, node); }); } @@ -272,25 +272,12 @@ class Usb_filter::Device_registry Entry const &) { xml.node("device", [&] { - char buf[16]; - unsigned const bus = _get_value(node, "bus"); unsigned const dev = _get_value(node, "dev"); - snprintf(buf, sizeof(buf), "usb-%d-%d", bus, dev); - xml.attribute("label", buf); + xml.attribute("label", Label("usb-", bus, "-", dev)); - snprintf(buf, sizeof(buf), "0x%4x", _get_value(node, "vendor_id")); - xml.attribute("vendor_id", buf); - - snprintf(buf, sizeof(buf), "0x%4x", _get_value(node, "product_id")); - xml.attribute("product_id", buf); - - snprintf(buf, sizeof(buf), "0x%4x", bus); - xml.attribute("bus", buf); - - snprintf(buf, sizeof(buf), "0x%4x", dev); - xml.attribute("dev", buf); + _gen_dev_attributes(xml, node); }); } diff --git a/repos/os/src/drivers/acpi/acpi.cc b/repos/os/src/drivers/acpi/acpi.cc index a62349abda..3e4ad11fa6 100644 --- a/repos/os/src/drivers/acpi/acpi.cc +++ b/repos/os/src/drivers/acpi/acpi.cc @@ -1605,9 +1605,7 @@ class Acpi_table static void attribute_hex(Xml_generator &xml, char const *name, unsigned long long value) { - char buf[32]; - Genode::snprintf(buf, sizeof(buf), "0x%llx", value); - xml.attribute(name, buf); + xml.attribute(name, String<32>(Hex(value))); } diff --git a/repos/os/src/drivers/virtdev_rom/main.cc b/repos/os/src/drivers/virtdev_rom/main.cc index bd57092028..15a72fe01e 100644 --- a/repos/os/src/drivers/virtdev_rom/main.cc +++ b/repos/os/src/drivers/virtdev_rom/main.cc @@ -151,9 +151,9 @@ struct Virtdev_rom::Main xml.node("device", [&] () { - static char name[DEVICE_NAME_LEN]; - snprintf(name, sizeof(name), "%s%u", _name_for_id(id), device_type_idx[id - 1]++); - xml.attribute("name", name); + using Name = String; + + xml.attribute("name", Name(_name_for_id(id), device_type_idx[id - 1]++)); xml.attribute("type", _name_for_id(id)); xml.node("io_mem", [&] () { xml.attribute("address", addr); diff --git a/repos/os/src/lib/vfs/file_system_factory.cc b/repos/os/src/lib/vfs/file_system_factory.cc index 2d86396e94..d197a6db58 100644 --- a/repos/os/src/lib/vfs/file_system_factory.cc +++ b/repos/os/src/lib/vfs/file_system_factory.cc @@ -116,11 +116,7 @@ Vfs::Global_file_system_factory::_try_create(Vfs::Env &env, */ Library_name Vfs::Global_file_system_factory::_library_name(Node_name const &node_name) { - char lib_name [Library_name::capacity()]; - Genode::snprintf(lib_name, sizeof(lib_name), "vfs_%s.lib.so", - node_name.string()); - - return Library_name(lib_name); + return Library_name("vfs_", node_name, ".lib.so"); } diff --git a/repos/os/src/lib/vfs/rtc_file_system.h b/repos/os/src/lib/vfs/rtc_file_system.h index a3a656f6d2..172e9ac3bf 100644 --- a/repos/os/src/lib/vfs/rtc_file_system.h +++ b/repos/os/src/lib/vfs/rtc_file_system.h @@ -16,6 +16,7 @@ /* Genode includes */ #include +#include #include #include @@ -60,10 +61,31 @@ class Vfs::Rtc_file_system : public Single_file_system Rtc::Timestamp ts = _rtc.current_time(); - char buf[TIMESTAMP_LEN+1]; - char *b = buf; - size_t n = Genode::snprintf(buf, sizeof(buf), "%04u-%02u-%02u %02u:%02u:%02u\n", - ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second); + struct Padded + { + unsigned pad, value; + + void print(Genode::Output &out) const + { + using namespace Genode; + + unsigned const len = printed_length(value); + if (len < pad) + Genode::print(out, Repeated(pad - len, Char('0'))); + + Genode::print(out, value); + } + }; + + String string { Padded { 4, ts.year }, "-", + Padded { 2, ts.month }, "-", + Padded { 2, ts.day }, " ", + Padded { 2, ts.hour }, ":", + Padded { 2, ts.minute }, ":", + Padded { 2, ts.second }, "\n" }; + char const *b = string.string(); + size_t n = string.length(); + n -= size_t(seek()); b += seek(); diff --git a/repos/os/src/server/terminal_log/main.cc b/repos/os/src/server/terminal_log/main.cc index 4c69596b5d..aa9576835d 100644 --- a/repos/os/src/server/terminal_log/main.cc +++ b/repos/os/src/server/terminal_log/main.cc @@ -26,11 +26,13 @@ namespace Genode { { public: - enum { LABEL_LEN = 64 }; + enum { LABEL_LEN = 64u }; private: - char _label[LABEL_LEN]; + using Label = Genode::String; + Label const _label; + Terminal::Connection &_terminal; public: @@ -39,7 +41,10 @@ namespace Genode { * Constructor */ Termlog_component(const char *label, Terminal::Connection &terminal) - : _terminal(terminal) { snprintf(_label, LABEL_LEN, "[%s] ", label); } + : + _label("[", label, "] "), + _terminal(terminal) + { } /***************** @@ -76,7 +81,7 @@ namespace Genode { return; } - _terminal.write(_label, strlen(_label)); + _terminal.write(_label.string(), strlen(_label.string())); _terminal.write(string, len); /* if last character of string was not a line break, add one */ diff --git a/repos/os/src/test/clipboard/main.cc b/repos/os/src/test/clipboard/main.cc index 9934e257e2..772575b34b 100644 --- a/repos/os/src/test/clipboard/main.cc +++ b/repos/os/src/test/clipboard/main.cc @@ -100,12 +100,7 @@ class Test::Subsystem bool _expect_import = true; - Label _session_label() - { - char buf[Label::capacity()]; - snprintf(buf, sizeof(buf), "%s -> clipboard", _name.string()); - return Label(Cstring(buf)); - } + Label _session_label() { return Label(_name, " -> clipboard"); } Attached_rom_dataspace _import_rom; diff --git a/repos/os/src/test/vfs_stress/main.cc b/repos/os/src/test/vfs_stress/main.cc index c208fa4bee..e0f6147c53 100644 --- a/repos/os/src/test/vfs_stress/main.cc +++ b/repos/os/src/test/vfs_stress/main.cc @@ -542,7 +542,7 @@ void Component::construct(Genode::Env &env) vfs_env.io().commit_and_wait(); }; - char path[Vfs::MAX_PATH_LEN]; + String path { }; MAX_DEPTH = config_xml.attribute_value("depth", 16U); @@ -563,11 +563,11 @@ void Component::construct(Genode::Env &env) elapsed_ms = timer.elapsed_ms(); for (int i = 0; i < ROOT_TREE_COUNT; ++i) { - snprintf(path, 3, "/%d", i); + path = { "/", i }; Vfs::Vfs_handle *dir_handle; - vfs_root.opendir(path, true, &dir_handle, heap); + vfs_root.opendir(path.string(), true, &dir_handle, heap); dir_handle->close(); - Mkdir_test test(vfs_root, heap, path); + Mkdir_test test(vfs_root, heap, path.string()); count += test.wait(); } elapsed_ms = timer.elapsed_ms() - elapsed_ms; @@ -590,8 +590,8 @@ void Component::construct(Genode::Env &env) elapsed_ms = timer.elapsed_ms(); for (int i = 0; i < ROOT_TREE_COUNT; ++i) { - snprintf(path, 3, "/%d", i); - Populate_test test(vfs_root, heap, path); + path = { "/", i }; + Populate_test test(vfs_root, heap, path.string()); count += test.wait(); } @@ -621,8 +621,8 @@ void Component::construct(Genode::Env &env) elapsed_ms = timer.elapsed_ms(); for (int i = 0; i < ROOT_TREE_COUNT; ++i) { - snprintf(path, 3, "/%d", i); - Write_test test(vfs_root, heap, path, vfs_env.io()); + path = { "/", i }; + Write_test test(vfs_root, heap, path.string(), vfs_env.io()); count += test.wait(); } @@ -657,8 +657,8 @@ void Component::construct(Genode::Env &env) elapsed_ms = timer.elapsed_ms(); for (int i = 0; i < ROOT_TREE_COUNT; ++i) { - snprintf(path, 3, "/%d", i); - Read_test test(vfs_root, heap, path, vfs_env.io()); + path = { "/", i }; + Read_test test(vfs_root, heap, path.string(), vfs_env.io()); count += test.wait(); } @@ -693,8 +693,8 @@ void Component::construct(Genode::Env &env) elapsed_ms = timer.elapsed_ms(); for (int i = 0; i < ROOT_TREE_COUNT; ++i) { - snprintf(path, 3, "/%d", i); - Unlink_test test(vfs_root, heap, path, vfs_env.io()); + path = { "/", i }; + Unlink_test test(vfs_root, heap, path.string(), vfs_env.io()); count += test.wait(); }