6 ms·
Classic array bounds error. Obviously, if argc == 0, you should not dereference anything in argv. It also shows why C is such a hard programming language. Ther
by phicoh 3y ago
Classic array bounds error. Obviously, if argc == 0, you should not dereference anything in argv.
It also shows why C is such a hard programming language. There is nothing to help the programmer in this case. The compiler doesn't know that argc is the length of argv, so there is not even a warning if you do it wrong.
- bloak 3y ago"Obviously, if argc == 0, you should not dereference anything in argv." I'd say that's not obvious. Normally argv is null-terminated so you can read argv[argc], and it's zero. There's no need to read argv[argc] if you've already looked at argc but sometimes it's more convenient to rely on the sentinel value.
- phicoh 3y agoIn my opinion that is like saying it might more convenient to use sprintf. Yes, that null has to be there for historical reasons. It doesn't mean it is a good idea to write code that relies on it.
- bloak 3y agoIt seems that (some version of) the C standard contains these words: argv[argc] shall be a null pointer So I wouldn't feel too bad about relying on it.
- lelanthran 3y ago> Yes, that null has to be there for historical reasons. It doesn't mean it is a good idea to write code that relies on it. That null has to be there because if it isn't then the implementation is broken, not the program. If you are coding defensively because the implementation might be broken, then you can't rely on argc being positive either
- planede 3y ago> Obviously, if argc == 0, you should not dereference anything in argv. Quoting ISO C17: > argv[argc] shall be a null pointer. Not constrained on argc being greater than zero (unlike the points that follow). So argv should always be dereferenceable within the program. It's an other question if this should be enforced by the Linux kernel or the C runtime. https://cigix.me/c17#5.1.2.2.1.p2 https://cigix.me/c17#5.1.2.2.1.p2
- JdeBP 3y agoYou are looking at the wrong standard. Look at the one for the operating systems' APIs.
- planede 3y agoIf you are writing a C program, then you should consult the C standard, and possibly POSIX. The Linux kernel + C runtime + C compiler should form a conforming implementation of the C language. Now if the Linux kernel doesn't promise to pass a non-NULL argv (and documents so), then it should be the responsibility of the C runtime to fix that once the program reaches "main". But as I wrote earlier, it's a whole other question.
- JdeBP 3y agoSo now go and actually consult the POSIX standard, as I just said. You clearly haven't.
- masfuerte 3y agoSo I just looked at the POSIX standard for the exec functions. It says the argv array is terminated by a null pointer when it arrives at the C main function. But a null argv doesn't cause exec to fail. Which implies that the OS or C runtime must fix it up between exec and main.
- planede 3y ago
- teo_zero 3y agoBut you don't have argc, only argv. The C stub will derive argc by traversing argv and then pass both to main().