Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strlen usage #10

Merged
merged 5 commits into from
Jan 21, 2025
Merged

Fix strlen usage #10

merged 5 commits into from
Jan 21, 2025

Conversation

kimkulling
Copy link
Owner

@kimkulling kimkulling commented Jan 21, 2025

Summary by CodeRabbit

  • Refactor
    • Updated driver query function to use modern C++ std::string_view for improved string handling
    • Simplified function signature and internal string comparison logic
    • Enhanced code clarity and efficiency by removing manual length management

Copy link

coderabbitai bot commented Jan 21, 2025

Warning

Rate limit exceeded

@kimkulling has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 20 minutes and 3 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 5f64a63 and bb26562.

📒 Files selected for processing (1)
  • src/backends/sdl2_renderer.cpp (4 hunks)

Walkthrough

The pull request introduces changes to the queryDriver function in the SDL2 renderer backend. The modification involves updating the function's signature to use std::string_view instead of a raw C-style string with a maximum length parameter. This change simplifies the string handling by leveraging modern C++ string view capabilities, removing explicit length management, and streamlining the comparison logic.

Changes

File Change Summary
src/backends/sdl2_renderer.cpp - Updated queryDriver function signature from (const char*, size_t) to (const std::string_view&)
- Replaced null pointer check with empty() method
- Simplified string comparison using string_view methods

Poem

🐰 A Rabbit's Ode to String Views

In C++'s garden, a view so bright,
No more lengths to measure with might
string_view dances, clean and lean
Simplifying code, a programmer's dream!
Farewell, old pointers, your time has passed 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/backends/sdl2_renderer.cpp (1)

Line range hint 201-203: Consider modernizing other string length calculations

Since we're modernizing string handling, consider updating the strlen usage in text alignment calculation to use string_view or string length methods for consistency.

Here's a suggested improvement:

-            Message_rect.x = r.x1 + surfaceMessage->clip_rect.w - font->mSize * strlen(string);
+            const auto text_length = std::string_view(string).length();
+            Message_rect.x = r.x1 + surfaceMessage->clip_rect.w - font->mSize * text_length;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f40cf68 and 5f64a63.

📒 Files selected for processing (1)
  • src/backends/sdl2_renderer.cpp (4 hunks)
🧰 Additional context used
🪛 GitHub Check: SonarCloud
src/backends/sdl2_renderer.cpp

[warning] 77-77: std::string_view::data() should not be passed to API expecting C-style strings

Fix this function call that expects null-terminated strings but use string_view::data().

See more on SonarQube Cloud

🪛 GitHub Actions: CMake
src/backends/sdl2_renderer.cpp

[error] 267-267: 'std__string' was not declared in this scope. The correct type name should be 'std::string' (with single underscore).

🔇 Additional comments (1)
src/backends/sdl2_renderer.cpp (1)

30-30: LGTM! Good modernization of the code.

The change to use std::string_view is a good improvement that:

  • Provides a more efficient way to handle string views without copying
  • Removes the need for explicit length management
  • Aligns with modern C++ practices

Also applies to: 66-66

@@ -267,7 +264,7 @@
return ErrorCode;
}

const int driverIndex = queryDriver(ctx, "opengl", 256);
const int driverIndex = queryDriver(ctx, std__string("opengl"));
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo in std::string

There's a typo in the type name: std__string should be std::string.

Apply this fix:

-    const int driverIndex = queryDriver(ctx, std__string("opengl"));
+    const int driverIndex = queryDriver(ctx, std::string("opengl"));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const int driverIndex = queryDriver(ctx, std__string("opengl"));
const int driverIndex = queryDriver(ctx, std::string("opengl"));
🧰 Tools
🪛 GitHub Actions: CMake

[error] 267-267: 'std__string' was not declared in this scope. The correct type name should be 'std::string' (with single underscore).

len = maxLen;
}
if (strncmp(driverType, info.name, len) == 0) {
if (strncmp(driverType.data(), info.name, driverType.size()) == 0) {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix potential buffer overrun with string_view::data()

Using string_view::data() with strncmp is unsafe as it doesn't guarantee null-termination. This could lead to buffer overrun if the string_view points to a non-null-terminated string.

Consider these safer alternatives:

-        if (strncmp(driverType.data(), info.name, driverType.size()) == 0) {
+        // Option 1: Convert to std::string to ensure null-termination
+        if (std::string(driverType) == info.name) {
+
+        // Option 2: Use string_view's compare if info.name length is known
+        if (driverType == std::string_view(info.name)) {

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 GitHub Check: SonarCloud

[warning] 77-77: std::string_view::data() should not be passed to API expecting C-style strings

Fix this function call that expects null-terminated strings but use string_view::data().

See more on SonarQube Cloud

Copy link

@kimkulling kimkulling merged commit 9a44779 into main Jan 21, 2025
3 checks passed
@kimkulling kimkulling deleted the fix/remove_strlen branch January 21, 2025 20:24
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.

1 participant