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

Range-ify std.internal.cstring 2nd try #3415

Merged
merged 1 commit into from Jun 17, 2015

Conversation

WalterBright
Copy link
Member

The same code as for #3404. The https://issues.dlang.org/show_bug.cgi?id=14696 issue is not caused by this PR, and the existing cstring.d it replaces has the same latent issue. Also, 14696 should not hold up this PR, and the Phobos problem has been worked around with #3413

else
{
To[256 / To.sizeof] _buff; // the 'small string optimization'
}
Copy link
Member

Choose a reason for hiding this comment

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

As I said in the original pull. This cannot be allowed. It changes the layout for importing modules based on whether -unittest is defined, but will link against the non-unittest version if it's using standard phobos library. This WILL cause buffer overflows, as the caller will reserve only 16 bytes for the stack space, but the function will run thinking it has 256 bytes.

I ran a simple test:

mod1.d:

import mod2;
void main()
{
    auto x = foo();
}

mod2.d:

module mod2;
auto foo() {
    static struct S {
        version(unittest)
            char[16] x;
        else
            char[256] x;
    }
    return S();
}

results:

$ dmd mod1.d mod2.d
$ ./mod1
$ dmd -c mod2.d
$ dmd -unittest mod1.d mod2.o
$ ./mod1
Segmentation fault: 11

Copy link
Member Author

Choose a reason for hiding this comment

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

ok, I see what you mean. Will fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

// not confuse unprecise GC.
version (unittest)
{
enum buffLength = 16 / To.sizeof; // smaller size to trigger reallocations
Copy link
Member

Choose a reason for hiding this comment

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

Huh?

Copy link
Member Author

Choose a reason for hiding this comment

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

the comment applies

Copy link
Member

Choose a reason for hiding this comment

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

Oh, sorry, I missed that this is not actually used to set the size of the array, and thought it would still experience the issue Steven pointed out.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the private enum could be inside the function instead of the type, but I think there's no risk of this leaking outside the file somehow. So it works, thanks.

@dnadlinger
Copy link
Member

One other thing: Do we want to have a char element type template constraint on the Range type?

I would merge the pull request regardless because it is just an internal helper function at this point anyway, but the auto tester seems to be a bit slow on the uptake right now.

size_t i;

static if (isSomeString!From)
auto r = cast(const(CF)[])str;
Copy link
Member

Choose a reason for hiding this comment

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

I was curious about this on the last pull, but I didn't look at it until after it was merged so I didn't bother. But what is this part for?

Copy link
Member Author

Choose a reason for hiding this comment

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

makes it happy when str is an inout.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right. inout has many issues with generic code, we need to work on a solution for that.

@schveiguy
Copy link
Member

Auto-merge toggled on

schveiguy added a commit that referenced this pull request Jun 17, 2015
Range-ify std.internal.cstring 2nd try
@schveiguy schveiguy merged commit f012f7d into dlang:master Jun 17, 2015
@CyberShadow
Copy link
Member

This pull request introduced a regression:
https://issues.dlang.org/show_bug.cgi?id=14979

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants