Skip to content

Commit

Permalink
VirtualFileSystemHook: ensure string is properly null terminated
Browse files Browse the repository at this point in the history
When the VirtualFileSystem hook receives an error reading data from GVFS
over a named pipe, it will include the pipe message in the error
message. The pipe message is not necassarily null terminated, so we need
to append a null terminating character before passing it to string
formatting functions that expect a null terminated string.

This change makes sure the message is null terminated.
  • Loading branch information
jamill committed Aug 15, 2018
1 parent 06793f2 commit 32cb916
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions GVFS/GVFS.VirtualFileSystemHook/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ enum VirtualFileSystemErrorReturnCode
ErrorVirtualFileSystemProtocol = ReturnCode::LastError + 1,
};

const int PIPE_BUFFER_SIZE = 1024;

int main(int argc, char *argv[])
{
if (argc != 2)
Expand Down Expand Up @@ -39,7 +41,10 @@ int main(int argc, char *argv[])
die(ReturnCode::PipeWriteFailed, "Failed to write to pipe (%d)\n", error);
}

char message[1024];
// Allow for 1 extra character in case we need to
// null terminate the message, and the message
// is PIPE_BUFFER_SIZE chars long.
char message[PIPE_BUFFER_SIZE + 1];
unsigned long bytesRead;
int lastError;
bool finishedReading = false;
Expand All @@ -51,22 +56,23 @@ int main(int argc, char *argv[])
success = ReadFromPipe(
pipeHandle,
message,
sizeof(message),
PIPE_BUFFER_SIZE,
&bytesRead,
&lastError);

if (!success)
{
break;
}

messageLength = bytesRead;

if (firstRead)
{
firstRead = false;
if (message[0] != 'S')
{
message[bytesRead] = 0;
die(ReturnCode::PipeReadFailed, "Read response from pipe failed (%s)\n", message);
}

Expand Down

0 comments on commit 32cb916

Please sign in to comment.