Skip to content

Commit f764d1e

Browse files
author
Wael Yehia
committed
code review
1 parent b817019 commit f764d1e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

compiler-rt/lib/profile/InstrProfilingFile.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ static int mmapProfileForMerging(FILE *ProfileFile, uint64_t ProfileFileSize,
432432
lprofGetFileContentBuffer(ProfileFile, ProfileFileSize, ProfileBuffer);
433433

434434
if (ProfileBuffer->Status == MM_INVALID) {
435-
PROF_ERR("Unable to merge profile data, mmap failed: %s\n",
436-
strerror(errno));
435+
PROF_ERR("Unable to merge profile data: %s\n", "reading file failed");
437436
return -1;
438437
}
439438

compiler-rt/lib/profile/InstrProfilingUtil.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,12 @@ static int isMmapSafe(int Fd) {
325325
COMPILER_RT_VISIBILITY void lprofGetFileContentBuffer(FILE *F, uint64_t Length,
326326
ManagedMemory *Buf) {
327327
Buf->Status = MM_INVALID;
328-
329-
if (!F || isMmapSafe(fileno(F))) {
330-
Buf->Addr = mmap(NULL, Length, PROT_READ, MAP_SHARED | MAP_FILE,
331-
F ? fileno(F) : -1, 0);
332-
if (Buf->Addr != MAP_FAILED)
328+
if (isMmapSafe(fileno(F))) {
329+
Buf->Addr =
330+
mmap(NULL, Length, PROT_READ, MAP_SHARED | MAP_FILE, fileno(F), 0);
331+
if (Buf->Addr == MAP_FAILED)
332+
PROF_ERR("%s: mmap failed: %s\n", __func__, strerror(errno))
333+
else
333334
Buf->Status = MM_MMAP;
334335
return;
335336
}
@@ -338,20 +339,22 @@ COMPILER_RT_VISIBILITY void lprofGetFileContentBuffer(FILE *F, uint64_t Length,
338339
PROF_NOTE("Could not use mmap; using fread instead.%s\n", "");
339340

340341
void *Buffer = malloc(Length);
341-
342342
if (!Buffer) {
343343
PROF_ERR("%s: malloc failed: %s\n", __func__, strerror(errno));
344344
return;
345345
}
346-
if (fseek(F, 0L, SEEK_SET) != 0) {
347-
PROF_ERR("%s: fseek(0, SEEK_SET) failed: %s\n", __func__, strerror(errno));
346+
if (ftell(F) != 0) {
347+
PROF_ERR("%s: expecting ftell to return zero\n", __func__);
348+
free(Buffer);
348349
return;
349350
}
350351

351352
// Read the entire file into memory.
352353
size_t BytesRead = fread(Buffer, 1, Length, F);
353-
if (BytesRead != (size_t)Length || ferror(F)) {
354-
PROF_ERR("%s: fread failed: %s\n", __func__, strerror(errno));
354+
if (BytesRead != (size_t)Length) {
355+
PROF_ERR("%s: fread failed%s\n", __func__,
356+
feof(F) ? ", end of file reached" : "");
357+
free(Buffer);
355358
return;
356359
}
357360

@@ -372,6 +375,8 @@ void lprofReleaseBuffer(ManagedMemory *Buf, size_t Length) {
372375
PROF_ERR("%s: Buffer has invalid state: %d", __func__, Buf->Status);
373376
break;
374377
}
378+
Buf->Addr = NULL;
379+
Buf->Status = MM_INVALID;
375380
}
376381

377382
COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,

0 commit comments

Comments
 (0)