Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File copy via PipeTo can lead to infinite loop #25

Open
mathieugarcia opened this issue Nov 29, 2011 · 1 comment
Open

File copy via PipeTo can lead to infinite loop #25

mathieugarcia opened this issue Nov 29, 2011 · 1 comment

Comments

@mathieugarcia
Copy link

When copying a file via nglPath::Copy, nglIStream::PipeTo is used:

 while (( istate == eStreamReady || istate == eStreamWait ) && ( ostate == eStreamEnd || ostate == eStreamWait ))
  {    
    piped_in = Read( buffer, PIPE_BUF_SIZE, 1 );
    istate = this->GetState();

      while ( piped_in > 0 && ( ostate == eStreamEnd || ostate == eStreamWait ) )
      {
        piped_out = rTarget.Write( buffer, piped_in, 1 );

        ostate = rTarget.GetState();
        piped_in -= piped_out;
        total_piped += piped_out;
      }
  }

Let's see nglFile::Write:

    done = ngl_fwrite(pData, 1, (size_t)(WordCount * WordSize), mFD);

        // [stripped]

    if (WordSize == 1)
        return done;

        // [stripped]

    done /= WordSize;
    return done;
}

There, ngl_fwrite() call can return 0 or -1. If -1 is returned, done /= WordSize will result in done being 0.
Maybe all Write() wrappers should threat -1 as an error.

Going back into PipeTo, 0 can be returned from the above Write call:

   piped_in -= piped_out;
   total_piped += piped_out;

piped_in is never decreased, resulting in an infinite loop.

@mathieugarcia
Copy link
Author

Will typically happen when no space is left on device.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant