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

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

Choose a reason for hiding this comment

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

Looks good, but could be simplified a bit:

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

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.

@PetarKirov
Copy link
Contributor

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[].

@tchaloupka
Copy link
Contributor Author

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

@PetarKirov
Copy link
Contributor

better? :)

perfect ;)

@s-ludwig s-ludwig merged commit ce49484 into vibe-d:master Nov 7, 2018
@s-ludwig
Copy link
Member

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.

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.

3 participants