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 returning hostname from uname on posix #2220

Merged
merged 1 commit into from Nov 7, 2018

Conversation

@tchaloupka
Copy link
Contributor

@tchaloupka tchaloupka commented Oct 17, 2018

As seen in documentation nodename is char[] of unspecified size and string is null terminated.

There is specified size of 65 in druntime.

But to!string on the char array doesn't handle null terminated string but just returns it in a full length with nulls.

I've found this out on one of the servers where something strange was logged and it was exactly 65 chars in length.

Fixed it using std.string.fromStringz.

Note: This should be fixed in vibe-core too

Copy link
Contributor

@PetarKirov PetarKirov left a comment

Looks good, but could be simplified a bit:

Loading

utsname name;
if (uname(&name)) return hostName;
hostName = name.nodename.to!string();
hostName = (&name.nodename[0]).fromStringz().idup;
Copy link
Contributor

@PetarKirov PetarKirov Oct 17, 2018

Choose a reason for hiding this comment

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

Simply name.nodename.ptr.to!string should do the trick.

Loading

@PetarKirov
Copy link
Contributor

@PetarKirov PetarKirov commented Oct 17, 2018

But to!string on the char array doesn't handle null terminated string but just returns it in a full length with nulls.

to!string handles null terminated strings, we just need to pass char*, instead of char[].

Loading

@tchaloupka
Copy link
Contributor Author

@tchaloupka tchaloupka commented Oct 17, 2018

Great, didn't know that, better? :)

Loading

@PetarKirov
Copy link
Contributor

@PetarKirov PetarKirov commented Oct 17, 2018

better? :)

perfect ;)

Loading

@s-ludwig s-ludwig merged commit ce49484 into vibe-d:master Nov 7, 2018
1 of 2 checks passed
Loading
@s-ludwig
Copy link
Member

@s-ludwig s-ludwig commented Nov 7, 2018

Thanks for the fix, I'll check vibe-core now. The documentation is quite confusing, considering that char[] is equivalent to char* in C.

Loading

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

Successfully merging this pull request may close these issues.

None yet

3 participants