# V1035. Only values returned from fgetpos() can be used as arguments to fsetpos().

As defined by the C standard, calling the 'fsetpos' function with an argument obtained not by the 'fgetpos' function leads to undefined behavior.

Consider the following example:

    int foo()
    {
      FILE * pFile;
      fpos_t position;

      pFile = fopen("file.txt", "w");
      memset(&position, 0, sizeof(position));
      fputs("That is a sample", pFile);
      fsetpos(pFile, &position);
      fputs("This", pFile);
      fclose(pFile);
      return 0;
    }

Because the value of the 'position' variable was obtained not by the 'fgetpos' function, further program execution may fail.

Fixed code:

    int foo()
    {
      FILE * pFile;
      fpos_t position;

      pFile = fopen("file.txt", "w");
      fgetpos(pFile, &position);
      fputs("That is a sample", pFile);
      fsetpos(pFile, &position);
      fputs("This", pFile);
      fclose(pFile);
      return 0;
    }

This diagnostic is classified as:

CERT-FIO44-C