Hacker News new | past | comments | ask | show | jobs | submit login

Because you haven't grasped it either. No-one is saying that argv[argc] != NULL. But in the case being discussed by M. Siebennmann, argc is 0.

The Single Unix Specification says, very clearly:

> The argument arg0 should point to a filename string that is associated with the process being started by one of the exec functions.

> The value in argv[0] should point to a filename string that is associated with the process being started by one of the exec functions.

Then it goes on at length in the rationale at the bottom of that same section about "the use of the word should" and even explains how programs are tripped up by argc being 0.

As you can see, both masfuerte and planede have still clearly not read this, despite that it's said twice in the description, and then has two entire paragraphs devoted to the reasoning underpinning it in the rationale.




I really don't know what you're trying to argue here.

Yes, that spec places additional requirements on top. But looking at just the C spec is apparently enough to find a violation here, and that's relevant! (Especially because the C spec doesn't say "should".)

People wanting to point that error out doesn't mean they're failing to grasp anything.


Think it through. Yes, both you and xe haven't grasped this. M. Siebennmann's scenario is that argc is 0, and argv[0] is NULL. There's nothing in the quoted

> argv[argc] shall be a null pointer.

constraint that that violates. argv[argc] is a null pointer. You have not found a violation of that constraint.

The constraint that is violated is, rather, in the Single Unix Specification. It's the SUS that puts the constraint upon how execve() may be invoked by a strictly conformant application, and requires that (as it says twice) argc be "one or greater", because it constrains the first element of the argument vector to be a pointer to a string, not a null pointer. The _only_ leeway is the wiggle room in "associated with", which it devotes an entire paragraph to explaining.

More than one standard applies. The idea that only the C standard covers the writing of these programs is wholly wrongheaded. After all, the C standard also allows non-POSIX implementations.


There are two scenarios here.

One scenario is that argc is 0 and argv[0] is NULL.

The other scenario is that argc is 0, argv is NULL, and argv[0] does not exist.

The C standard makes the second scenario invalid, but Linux was allowing it.

This is all directly relevant to the first two posts in the comment thread, talking about "relying on magic values (NULL termination)" and "Obviously, if argc == 0, you should not dereference anything in argv. [...] shows why C is such a hard programming language". Even though they were talking about C by itself, that's actually incorrect, C guarantees the null termination and this rule was being broken by the OS.


While we are at it [0]:

> The meanings specified in POSIX.1-2017 for the words shall, should, and may are mandated by ISO/IEC directives.

By those directives "should" indicates recommendation. [1]

> In POSIX.1-2017, the word should does not usually apply to the implementation, but rather to the application. Thus, the important words regarding implementations are shall, which indicates requirements, and may, which indicates options.

You also have to differentiate between any requirements for the argv argument when passed to one of the exec functions and the argv parameter received in main. Any requirement for the former constrains the application, but requirements for the latter constrains the implementation.

exec is indeed only documented in POSIX and OS specific documentations, but main is also documented in ISO C. Both POSIX and ISO C constrains the argv parameter of main not to be null. They don't require argc > 0 or argv[0] != null.

From [2]:

> [under exec] The argument argv is an array of character pointers to null-terminated strings.

This is a hard requirement, without "should". This implies that argv must not be null itself when passed to exec. This probably gives implementations the freedom to do whatever in this case, POSIX doesn't define the behavior. The requirement for argv[0] (which implies argc > 0) is merely a recommendation.

[0] https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xbd...

[1] https://www.iso.org/foreword-supplementary-information.html




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: