简体   繁体   中英

is it essential to copy a returned struct from kernel space to user space?

if I make a system call, say

struct hostent * gethostbyaddr (const char *addr, int len, int family );

and it returns a struct* , do i need to copy the struct to somewhere I "own" instead of using the pointer directly?

The documentation of gethostbyaddr reads (see Notes section):

The functions gethostbyname() and gethostbyaddr() may return pointers to static data, which may be overwritten by later calls. Copying the struct hostent does not suffice, since it contains pointers; a deep copy is required.

It means that you need to copy the struct if you want to be sure that it won't be overwritten by subsequent calls to gethostbyaddr .

But you really need to consider using getaddrinfo since gethostbyaddr is obsolete. With getaddrinfo you will not be asking this question because you'll need to allocate memory for the result yourself.

No, you can use the pointer directly.

(Think about it: if the memory was inaccessible or unsafe to access, you won't be able to copy it; and vice versa, if you are able to copy it, you can as well use it inplace).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM