Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p><em>but it can be proven that when adding only one to the length of the write, the program will never crash.</em></p> </blockquote> <p>No! You can't prove that! Not in the sense of a mathematical proof. You have only shown that on your system, with your compiler, with those particular compiler settings you used, with particular environment configuration, it <strong>might</strong> not crash. This is far from a mathematical proof! </p> <p>In fact the C standard itself, although it guarantees that you can get the address of "one place after the last element of an array", it also states that dereferencing that address (i.e. trying to read or write from that address) is <em>undefined behaviour</em>. </p> <p>That means that an implementation can do <strong>everything</strong> in this case. It can even do what you expect with naive reasoning (i.e. work - but it's sheer luck), but it may also crash or it may also format your HD (if your are very, very unlucky). This is especially true when writing system software (e.g. a device driver or a program running on the bare metal), i.e. when there is no OS to shield you from the nastiest consequences of writing bad code!</p> <p><strong>Edit</strong> This should answer the question made in a comment (C99 draft standard):</p> <blockquote> <h3>7.19.7.2 The fgets function</h3> <p><strong>Synopsis</strong></p> <pre><code>#include &lt;stdio.h&gt; char *fgets(char * restrict s, int n, FILE * restrict stream); </code></pre> <p><strong>Description</strong></p> <p>The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.</p> <p><strong>Returns</strong></p> <p>The fgets function returns s if successful. If end-of-file is encountered and no characters have been read into the array, the contents of the array remain unchanged and a null pointer is returned. If a read error occurs during the operation, the array contents are indeterminate and a null pointer is returned.</p> </blockquote> <p><strong>Edit</strong>: Since it seems that the problem lies in a misunderstanding of what a string is, this is the relevant excerpt from the standard (emphasis mine):</p> <blockquote> <h3>7.1.1 Definitions of terms</h3> <p>A string is a contiguous sequence of characters <strong>terminated by and including the first null character</strong>. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The <strong>length of a string</strong> is the number of bytes <strong>preceding</strong> the null character and the value of a string is the sequence of the values of the contained characters, in order.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload