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

Support c_str() for compatibility with legacy C-Style APIs #37

Closed
shreyasbharath opened this issue May 11, 2020 · 5 comments
Closed

Support c_str() for compatibility with legacy C-Style APIs #37

shreyasbharath opened this issue May 11, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@shreyasbharath
Copy link

shreyasbharath commented May 11, 2020

Imagine a scenario like the below test. Because magic_enum returns a string_view, it is not compatible with C-style APIs.

Can we do anything to support legacy C-style code? The below code will result in a read buffer overflow as it'll keep reading past the end of the string until it hits a null terminator.

TEST("Read buffer overflow bug")
{
    enum class TestEnum
    {
        MODE_1,
        MODE_2
    };

    TestEnum test = TestEnum::MODE_2;
    auto name = magic_enum::enum_name(test);
    printf("%s", name.data());
}
@Neargye Neargye self-assigned this May 11, 2020
@Neargye Neargye added the enhancement New feature or request label May 11, 2020
@Neargye
Copy link
Owner

Neargye commented May 11, 2020

Hi @shreyasbharath, update in master.
Now example code is work ok.

@shreyasbharath
Copy link
Author

Wow @Neargye thank you for the quick fix/response.

Can you also please add some tests/documentation on how to use? Can we create an overloaded function that returns a const char*? Thanks.

@Neargye
Copy link
Owner

Neargye commented May 11, 2020

I would not want to add a bunch of overloads. Now the library stores zero terminated string, so taking a string through the data() will work correctly.

@Neargye Neargye closed this as completed May 11, 2020
@Neargye
Copy link
Owner

Neargye commented May 11, 2020

add some notices to doc

@shreyasbharath
Copy link
Author

Thanks again 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants