Skip to content

Cleanup str(n)cmp with std::string_view #4567#4742

Closed
sivansh11 wants to merge 4 commits intocanonical:mainfrom
sivansh11:fix/4567
Closed

Cleanup str(n)cmp with std::string_view #4567#4742
sivansh11 wants to merge 4 commits intocanonical:mainfrom
sivansh11:fix/4567

Conversation

@sivansh11
Copy link
Copy Markdown

Closes #4567

replaces error prone str(n)cmp with more expressive std::string_view

@sivansh11 sivansh11 requested a review from a team as a code owner March 4, 2026 15:21
@sivansh11
Copy link
Copy Markdown
Author

@tarek-y-ismail, I am not able to test the changes, since ctest is getting stuck on the 6th test (named wlcs)
the test is getting stuck even before my current commit

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR modernizes C-string comparisons across Mir by replacing strcmp/strncmp usages with clearer std::string_view equality and starts_with() checks, aligning with #4567’s guidance.

Changes:

  • Replaced strcmp equality/inequality checks with std::string_view comparisons (often using "..."sv literals).
  • Replaced common strncmp(prefix, ..., N) patterns with std::string_view::starts_with().
  • Added <string_view> includes (and std::string_view_literals where needed) in many touched files.

Reviewed changes

Copilot reviewed 28 out of 28 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/unit-tests/input/evdev/test_evdev_input_platform.cpp Switches devnode matching from strcmp to std::string_view compare (currently missing <string_view> include).
tests/unit-tests/console/test_minimal_console_services.cpp Replaces /dev/ + /sys comparisons with starts_with() / std::string_view equality; adds <string_view>.
tests/unit-tests/console/test_linux_virtual_terminal.cpp Replaces /dev/ prefix check with starts_with(); adds <string_view>.
tests/miral/wayland_extensions.cpp Replaces Wayland interface name strcmp checks with std::string_view comparisons; adds <string_view>.
tests/miral/external_client.cpp Replaces getenv/strcmp check with safe pointer check + string_view literal compare; adds <cstdlib>, <string_view>, and literals.
tests/mir_test_framework/test_wlcs_display_server.cpp Replaces Wayland resource class strcmp checks with string_view/literal comparisons; adds <string_view> and literals.
tests/mir_test_doubles/mock_drm.cpp Replaces /dev/dri/ prefix strncmp with starts_with(); adds <string_view>.
src/server/scene/basic_surface.cpp Replaces strcmp on pthread name with string_view literal compare; adds <string_view> and literals.
src/server/graphics/platform_probe.cpp Replaces strcmp ordering / module name checks with std::string_view comparisons; adds <string_view> and literals.
src/server/console/minimal_console_services.cpp Replaces DEVNAME= strncmp parsing with starts_with(); adds <string_view>.
src/server/console/logind_console_services.cpp Replaces several strcmp/strncmp checks with string_view equality/starts_with(); adds <string_view> and literals.
src/server/console/linux_virtual_terminal.cpp Replaces DEVNAME= strncmp parsing with starts_with(); adds <string_view>.
src/platforms/wayland/displayclient.cpp Replaces Wayland global interface strcmp checks with string_view/literal comparisons; adds <string_view> and literals.
src/platforms/gbm-kms/server/kms/real_kms_output.cpp Replaces DRM property/mode strcmp checks with string_view literal comparisons; adds <string_view> and literals.
src/platforms/gbm-kms/server/kms/platform_symbols.cpp Replaces driver string comparisons and prefix checks with string_view compares/starts_with(); adds <string_view> and literals.
src/platforms/gbm-kms/server/kms/display.cpp Replaces devnode strcmp checks with std::string_view equality; adds <string_view>.
src/platforms/evdev/platform.cpp Replaces strncmp/strcmp checks with starts_with() / std::string_view equality; adds <string_view>.
src/platforms/eglstream-kms/server/egl_output.cpp Replaces DRM property strcmp check with string_view literal compare; adds <string_view> and literals.
src/platforms/atomic-kms/server/kms/display.cpp Replaces devnode strcmp checks with std::string_view equality; adds <string_view>.
src/platforms/atomic-kms/server/kms/atomic_kms_output.cpp Replaces mode strcmp check with string_view literal compare; adds <string_view> and literals.
src/platform/udev/udev_wrapper.cpp Replaces devpath equality and action string comparisons with std::string_view compares/literals; adds <string_view> and literals.
src/platform/options/default_configuration.cpp Replaces library name strcmp ordering check with std::string_view ordering; adds <string_view> and literals.
src/miral/wayland_app.cpp Replaces Wayland registry interface strcmp checks with string_view literal comparisons; adds <string_view> and literals.
src/miral/launch_app.cpp Replaces env-var prefix strncmp checks with starts_with(); adds <string_view>.
include/test/mir/test/event_matchers.h Replaces matcher strcmp with std::string_view inequality; adds <string_view>.
examples/miral-shell/shell_main.cpp Replaces strcmp against env-var values with string_view literal comparisons; includes <string_view> and literals.
examples/example-server-lib/wayland_app.cpp Replaces Wayland registry interface strcmp checks with string_view literal comparisons; includes <string_view> and literals.
examples/client/mir_shell_demo.cpp Replaces registry strcmp checks with "..."sv and std::string_view compares (currently missing <string_view> + literals import).

Comment thread tests/unit-tests/input/evdev/test_evdev_input_platform.cpp
Comment on lines +387 to 393
if (interface == "wl_compositor"sv)
{
globals::compositor = static_cast<wl_compositor*>(wl_registry_bind(
registry, id, &wl_compositor_interface, std::min(version, 3u)));
}
else if (strcmp(interface, "wl_shm") == 0)
else if (interface == "wl_shm"sv)
{
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block uses the "..."sv string_view literal and std::string_view{...} but the file doesn't include <string_view> nor import std::string_view_literals, so it will not compile. Add #include <string_view> and using namespace std::string_view_literals; (or avoid sv by comparing via std::string_view{interface} == "wl_compositor").

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block uses the "..."sv string_view literal and std::string_view{...} but the file doesn't include <string_view> nor import std::string_view_literals, so it will not compile. Add #include <string_view> and using namespace std::string_view_literals; (or avoid sv by comparing via std::string_view{interface} == "wl_compositor").

This only compiles because <string> transitively includes <string_view> in the current libstcc++ implementation. (Which then means using namespace std::literals pulls in string_view_literals.)

There's no guarantee this will work with other library implementations (nor future versions of this one).

{
// Starting an X server on LP builder, or Fedora CI, doesn't work
auto const lp_fake_runtime_dir = strcmp(getenv("XDG_RUNTIME_DIR"), "/tmp") == 0;
auto const xdg_dir = getenv("XDG_RUNTIME_DIR");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a subtle change here: the original code has undefined behaviour (probably a SIGSEGV) if getenv("XDG_RUNTIME_DIR") returns nullptr. The new code sets lp_fake_runtime_dir to false and carries on.

It would probably be better to abort() if there is no XDG_RUNTIME_DIR in the environment.

However, there are checks elsewhere that ensure XDG_RUNTIME_DIR is set, so this is likely moot.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlanGriffiths should I add the abort check here even tho it is rechecked later on ? Or maybe just assert ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sivansh11 in general, it is better to fail noisily (i.e. abort()) when things are wrong than to carry on (set lp_fake_runtime_dir to false, assert() or "undefined behaviour").

Comment thread tests/unit-tests/input/evdev/test_evdev_input_platform.cpp
Comment on lines +387 to 393
if (interface == "wl_compositor"sv)
{
globals::compositor = static_cast<wl_compositor*>(wl_registry_bind(
registry, id, &wl_compositor_interface, std::min(version, 3u)));
}
else if (strcmp(interface, "wl_shm") == 0)
else if (interface == "wl_shm"sv)
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Comment thread tests/miral/external_client.cpp Outdated
Comment on lines +87 to +88
if (!xdg_dir)
std::runtime_error("XDG_RUNTIME_DIR not found in environment");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the intention here? This constructs an unnamed runtime_error instance (and discards it): that doesn't achieve anything.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to throw, force pushed a fix to this

#include <string_view>

namespace mo = mir::options;
using namespace std::string_view_literals;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used?

Comment thread tests/unit-tests/input/evdev/test_evdev_input_platform.cpp
auto const rhs_desc = rhs->load_function<mir::graphics::DescribeModule>("describe_graphics_module", MIR_SERVER_GRAPHICS_PLATFORM_VERSION);

return strcmp(rhs_desc()->name, lhs_desc()->name) > 0;
return std::string_view{rhs_desc()->name} > std::string_view{lhs_desc()->name};
Copy link
Copy Markdown
Author

@sivansh11 sivansh11 Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used here @AlanGriffiths

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are you using a literal?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, I missed it

Comment on lines +87 to +88
if (!xdg_dir)
throw std::runtime_error("XDG_RUNTIME_DIR not found in environment");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing an exception likely results in an unhelpful message from the test framework:

Suggested change
if (!xdg_dir)
throw std::runtime_error("XDG_RUNTIME_DIR not found in environment");
if (!xdg_dir)
{
puts("This test expects XDG_RUNTIME_DIR to be set");
abort();
}

Comment on lines +387 to 393
if (interface == "wl_compositor"sv)
{
globals::compositor = static_cast<wl_compositor*>(wl_registry_bind(
registry, id, &wl_compositor_interface, std::min(version, 3u)));
}
else if (strcmp(interface, "wl_shm") == 0)
else if (interface == "wl_shm"sv)
{
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block uses the "..."sv string_view literal and std::string_view{...} but the file doesn't include <string_view> nor import std::string_view_literals, so it will not compile. Add #include <string_view> and using namespace std::string_view_literals; (or avoid sv by comparing via std::string_view{interface} == "wl_compositor").

This only compiles because <string> transitively includes <string_view> in the current libstcc++ implementation. (Which then means using namespace std::literals pulls in string_view_literals.)

There's no guarantee this will work with other library implementations (nor future versions of this one).

@AlanGriffiths
Copy link
Copy Markdown
Contributor

@sivansh11 have you abandoned this? You've not responded to review comments for a couple of weeks:

Closing for now: feel free to reopen if you can address the above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace str(n)?cmp with std::string_view::operator==

3 participants