0

I have a question: I try to use the systemcall gethostname( char *name, size_t size ) but I get some results I don't understand:

char *hostname;
gethostname( hostname, size );  //results in a segmentation error
gethostname( &hostname, size ); //works fine.

Why should I use the & sign here? gethostname asks for a pointer, but hostname is already a pointer. I just got the result by accident and have no idea why the first call doesn't work. Sorry if this is a dumb question.

[EDIT] The answer is: hostname did not allocate any memory yet and therefore, the gethostname tried to access non-allocated memory, raising the segmentation error. Furthermore, the second LOC did not work legally either, it overwrote other data on the stack and therefore corrupted it. The way to use it is to first allocate some memory and then pass it to gethostname systemcall.

Thanks for your time and information guys! :)

10
  • @OliCharlesworth Well, it writes to that array but won't modify the pointer. The OP simply didn't allocate any memory. So the first variant is perfect if the pointer points to a char buf of some kind. The second call should produce a warning and doesn't trap because the stack is within the prog's memory. Commented Jun 1, 2014 at 17:05
  • @OliCharlesworth - surely the first variant is fully valid (sans the unallocated storage problem) and shouldn't raise a warning. if it does, it would potentially be use-before-write. The second variant should definitely raise a warning, since you're now passing in a char**. Commented Jun 1, 2014 at 17:08
  • 1
    1 Define "works fine". How do you check? 2. The "size" thing. What does it do? Do you think it's there for extra aesthetic points? Commented Jun 1, 2014 at 17:09
  • @n.m. you are mean ;-). Commented Jun 1, 2014 at 17:10
  • @n.m. well, when I printf the hostname, it actually returns the name of the server I'm running my code on. Commented Jun 1, 2014 at 17:13

3 Answers 3

3

gethostname expects you to pass a buffer for it to write in to. In C, strings are implemented as an array of characters, which can decay to a pointer to the first character in the string.

What gethostname will do is to place the hostname in the buffer you specify, until it has written size characters. Given this, you need to give it a valid buffer to write into. In your example, nothing has yet been assigned to the pointer, hence the value of the pointer is undefined, and it's pointing to somewhere random in memory. Attempting to dereference that pointer is then undefined, and thus the program segfaults.

As I say, the way to fix this is to pass in a real area of storage. Both of the following approaches are valid:

char hostname[50];
gethostname(hostname, 50); // Valid as a char[] decays to char*
// ---------- OR ----------------
char hostname*;
hostname = malloc(sizeof(char)*50); // Actually give the pointer some storage.
gethostname(hostname, 50);

It should be noted that it worked in your second case by accident. char* hostname is a pointer that is stored on the stack. &hostname will then get the address of the pointer hostname. Since hostname has some valid storage on the stack (to store the address it points to), it is possible to write to this address, but you'll start overwriting useful data on the stack. This will then corrupt the state of the program and it'll probably crash or become unstable.

Sign up to request clarification or add additional context in comments.

Comments

2

Actual the 'works fine' line was also incorrect; it did not result in a seg fault but did write onto the stack where your pointer is actually located. In this case a seg fault did not occur because it is valid memory - but still illegal since it would corrupt any other automatic vars you would add to this program, and those vars would corrupt the name once used. Allocate real space as shown by others to fix.

Comments

1

There should be space allocated for the string hostname, unless this is in a function where hostname is a foreign string (declared in another function) as Oli points out

char hostname[128];

gethostname(hostname, sizeof hostname);
printf("My hostname: %s\n", hostname);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.