-1

I am reviewing the inet_ntop documentation. Typically, string builder operations in C return either a pointer to the end of the string, or an integer indicating the length of the string written.

The documentation of inet_ntop states quite ambiguously:

On success, inet_ntop() returns a non-null pointer to dst.  NULL is 
returned if there was an error, with the errno set to indicate the 
error.

There are two problems here:

  1. Null string termination: it is not clear whether I need to add a null string delimiter throughout the memory allocation where inet_ntop is printing when compiling at order 3 or more

  2. I do not know whether the output of the function returns a pointer to the last written character, or the first written character.


What does inet_ntop return?

Chris
  • 28,822
  • 27
  • 83
  • 158

3 Answers3

2

It's quite clear to me, but perhaps you need to apply some "good will" to get it:

  1. It's not a string in C if it isn't terminated, so of course the result is terminated.
  2. dst is an input argument, even if it's a bit weirdly written "a pointer to dst" can't be "a pointer to dst plus something".

You can of course also read an implementation to see what's going on. The last statement for the "happy path" (no buffer overflow) for both IPv4 and v6 variants is:

return strcpy(dst, tmp);

Which instantly tells you that dst is receiving a terminated string, and that dst is returned.

unwind
  • 391,730
  • 64
  • 469
  • 606
2

It returns const char * and since the dst parameter is required to be a valid object (you can not pass NULL as dst) there is no need to create an intermediate pointer to return the status of the function.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0

The inet_pton() documentation:

...

SYNOPSIS

#include <arpa/inet.h>

const char *inet_ntop(int af, const void *restrict src,
       char *restrict dst, socklen_t size);

...

DESCRIPTION

The inet_ntop() function shall convert a numeric address into a text string suitable for presentation. The af argument shall specify the family of the address. This can be AF_INET or AF_INET6. The src argument points to a buffer holding an IPv4 address if the af argument is AF_INET, or an IPv6 address if the af argument is AF_INET6; the address must be in network byte order. The dst argument points to a buffer where the function stores the resulting text string; it shall not be NULL. The size argument specifies the size of this buffer, which shall be large enough to hold the text string (INET_ADDRSTRLEN characters for IPv4, INET6_ADDRSTRLEN characters for IPv6).

...

RETURN VALUE

The inet_ntop() function shall return a pointer to the buffer containing the text string if the conversion succeeds, and NULL otherwise, and set errno to indicate the error.

...

ERRORS

The inet_ntop() and inet_pton() functions shall fail if:

[EAFNOSUPPORT] The af argument is invalid.

[ENOSPC] The size of the inet_ntop() result buffer is inadequate.

And the definition of a "string":

3.92 Character String

A contiguous sequence of characters terminated by and including the first null byte.

Given that, the answer to

  1. Null string termination: it is not clear whether I need to add a null string delimiter throughout the memory allocation where inet_ntop is printing when compiling at order 3 or more

should be clear. A string by definition includes the null byte. inet_pton() returns a pointer to a buffer that contains such a string. No addition of a null byte is needed.

And the answer to

  1. I do not know whether the output of the function returns a pointer to the last written character, or the first written character.

is given by the C standard in 6.3.2.3 Pointers, paragraph 7:

... When a pointer to an object is converted to a pointer to a character type, the result points to the lowest addressed byte of the object.

So, the "pointer to the buffer containing the text string if the conversion succeeds" returned by inet_ntop() points to the first byte in the string.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • You have not dealt with broken C code much, I take it. Nonetheless, I see this question is going to trigger a stack overflow "hammer session" – Chris Jun 26 '19 at 14:55
  • flagged. I get it, I promise: a string is a null delimeted char array. but these are not always null delimited by a human user, or function. I'm not saying I do it. But look: we write computer languages in computing language because English is imprecise. You are in denial over this simple fact, and blasting away on a question with an accepted answer. – Chris Jun 26 '19 at 15:07
  • Standards are written for implementors, not for users. Reference documentation like the `inet_ntop` manpage is written for people who already know all the ins and outs of the language and just need a reminder. Quoting such documents at someone who does not fully understand C yet, not only fails to answer their question, it teaches them that people who don't fully understand C are not welcome here. Kindly do not do this ever again. – zwol Jun 26 '19 at 15:09
  • 1
    @donlan No, you **DON'T** get it: *a string is a null delimeted char array. but these are not always null delimited.* It's not a "string" if it's missing the null character termination. The English here is not imprecise, you are. – Andrew Henle Jun 26 '19 at 15:09
  • @zwol *Standards are written for implementors* Writing code **is** implementing, and the standards are the reference. The POSIX standard is clearer than what I assume to be the Linux man page quoted in the question. I started with `inet_pton()` in POSIX, pulled in the POSIX string definition, and got to the point of answering his questions simply and directly from the relevant standards. It's a simple, direct answer in a way that demonstrated **where** it comes from. Care to explain what's wrong with that? – Andrew Henle Jun 26 '19 at 15:16
  • No, you have just misinterpretation my English: strings *are* null delimited. However, *students* may record strings in buffers that automatically get nulled at standard compilation orders, but yield memory faults at order 3 compilation and beyond. I encounter it all the time when I come in and clean up code. Thus, I am very wary of all the crazy things that can happen. – Chris Jun 26 '19 at 15:18
  • @donlan Your question isn't about student-written code. It's about `inet_ntop()`, a widely-used, standard library function. – Andrew Henle Jun 26 '19 at 15:19
  • In other words: char arrays that are incompletely filled default to being strings at low compilation orders. However, I must go in and correct memory leaks and read faults if, say, a string was not technically created, but rather a char array with no null delimitation. So you may very well not be experienced with broken C code, and my point may very well be astute. – Chris Jun 26 '19 at 15:20
  • @donlan Then make it clear you're dealing with such data. Your question is literally *What does inet_ntop return?* – Andrew Henle Jun 26 '19 at 15:21
  • You can’t desperately cry foul because I didn’t provide a memoir of context to the question. The Stack overflow code is minimal content needed for the question, and answer. I asked a question, and received an answer. But it seems you want to use this to get some kind of positive emotional transaction regarding your intellect. I am sure you are very bright. But I don’t have time for this. – Chris Jun 26 '19 at 15:22
  • @AndrewHenle I feel like I'm repeating myself here, but lemme try to put it clearer: Your answer is NOT a simple, direct answer. For anyone who does not already know how to read the style of documentation you are quoting, it is _totally incomprehensible_. Moreover, anyone who knows how to read this style of documentation _necessarily_ knows C well enough that they wouldn't have asked the question you're answering. Therefore your answer is not only useless, it's _hostile_. You are implying that no one who can't already read this documentation deserves to be allowed to ask questions here. – zwol Jun 26 '19 at 16:41