Skip to content

Commit

Permalink
fix warning about possibly uninitialized eof var in fileio.
Browse files Browse the repository at this point in the history
So my gcc 12.2 started complaining about possibly using an uninitialized variable
eof:
,----
| fileio.c: In function ‘readfile’:
| fileio.c:221:17: warning: ‘eof’ may be used uninitialized [-Wmaybe-uninitialized]
|   221 |     int         eof;
|       |                 ^~~
`----

It is currently only initialized in the `if (read_buffer)` case but not in the else clause.
There is the following code:

    if (read_buffer)
      eof = FALSE;
    else                  [1]
    {
      if (filesize == 0)
       [...]
      else if (filesize > 0 ...  [4]
      {
          [...]
	  if (curbuf->b_cryptstate->method_nr == CRYPT_M_SOD
					&& !eof && may_need_lseek)   [3]
	  {
          [...]

	  }
      }
      eof = size;        [2]

In the `else` condition [1], eof will only be initialized at [2],
while it looks like it may be used at [3].

So technically, the `eof` variable could be accessed uninitialized in
the else if case [4]. This can not happen, because filesize
is initialized to zero, so Vim cannot run into this case in practice
and after the first loop, eof will be initialized.

However, I think it's worth it, to move the initialization of the `eof`
variable to the beginning of the function and don't init it in both
branches of the if/else condition and also also do not depend on the
implicit initialization of the filesize variable.
  • Loading branch information
chrisbra committed Jun 16, 2023
1 parent 46acad7 commit 8811ec4
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions src/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ readfile(
int using_b_ffname;
int using_b_fname;
static char *msg_is_a_directory = N_("is a directory");
int eof;
int eof = FALSE;
#ifdef FEAT_SODIUM
int may_need_lseek = FALSE;
#endif
Expand Down Expand Up @@ -1229,7 +1229,6 @@ readfile(
* Read bytes from curbuf. Used for converting text read
* from stdin.
*/
eof = FALSE;
if (read_buf_lnum > from)
size = 0;
else
Expand Down

0 comments on commit 8811ec4

Please sign in to comment.