Skip to content

Commit 63b480b

Browse files
committed
Use open(), fstat() and fdopen() to prevent "time of check, time of use" problem
1 parent 9f49ae7 commit 63b480b

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/server.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,10 +820,14 @@ class Page
820820
while ( (d=readdir(dir))!=0 )
821821
{
822822
std::string subject=join_path(directory,d->d_name);
823-
const char* file = subject.c_str();
823+
int fd = open(subject.c_str(), O_RDONLY);
824824

825-
if (stat(file, &statbuf) ==-1)
825+
if (fd < 0)
826826
continue;
827+
if (fstat(fd, &statbuf) ==-1) {
828+
close(fd);
829+
continue;
830+
}
827831
if (S_ISDIR(statbuf.st_mode))
828832
{
829833
if ( (strcmp(d->d_name,".")!=0) && (strcmp(d->d_name,"..")!=0 ) )
@@ -836,9 +840,7 @@ class Page
836840
else
837841
{
838842
bool found = false;
839-
std::string str = subject;
840-
transform(str.begin(), str.end(),str.begin(), tolower );
841-
FILE *fp = fopen(file, "rb");
843+
FILE *fp = fdopen(fd, "rb");
842844
if (fp)
843845
{
844846
Pcap_file pfile(fp);
@@ -866,10 +868,10 @@ class Page
866868
printf(" %c{\n \"data\" : \"%s\",\n \"attr\" : { \"id\" : \"%s\", \"size\": %d, \"type\": \"json\" },\n \"children\" : [] }\n",
867869
comma,d->d_name,join_path(m_url.get_path(),d->d_name).substr(5).c_str(), int(statbuf.st_size) );
868870
comma= ',';
869-
found = true;
870871
}
871872
}
872873
}
874+
close(fd);
873875
}
874876

875877
printf("]\n");

0 commit comments

Comments
 (0)