summaryrefslogtreecommitdiff
path: root/gcc/gcov-io.c
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2017-05-02 14:43:35 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2017-05-02 14:43:35 +0000
commit34efdaf078b01a7387007c4e6bde6db86384c4b7 (patch)
treed503eaf41d085669d1481bb46ec038bc866fece6 /gcc/gcov-io.c
parentf733cf303bcdc952c92b81dd62199a40a1f555ec (diff)
downloadgcc-tarball-master.tar.gz
gcc-7.1.0gcc-7.1.0
Diffstat (limited to 'gcc/gcov-io.c')
-rw-r--r--gcc/gcov-io.c77
1 files changed, 28 insertions, 49 deletions
diff --git a/gcc/gcov-io.c b/gcc/gcov-io.c
index 17fcae0063..64dedd5528 100644
--- a/gcc/gcov-io.c
+++ b/gcc/gcov-io.c
@@ -1,5 +1,5 @@
/* File format for coverage information
- Copyright (C) 1996-2016 Free Software Foundation, Inc.
+ Copyright (C) 1996-2017 Free Software Foundation, Inc.
Contributed by Bob Manson <manson@cygnus.com>.
Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
@@ -115,10 +115,9 @@ static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
opened. If MODE is >= 0 an existing file will be opened, if
possible, and if MODE is <= 0, a new file will be created. Use
MODE=0 to attempt to reopen an existing file and then fall back on
- creating a new one. If MODE < 0, the file will be opened in
+ creating a new one. If MODE > 0, the file will be opened in
read-only mode. Otherwise it will be opened for modification.
- Return zero on failure, >0 on opening an existing file and <0 on
- creating a new one. */
+ Return zero on failure, non-zero on success. */
GCOV_LINKAGE int
#if IN_LIBGCOV
@@ -128,7 +127,7 @@ gcov_open (const char *name, int mode)
#endif
{
#if IN_LIBGCOV
- const int mode = 0;
+ int mode = 0;
#endif
#if GCOV_LOCKED
struct flock s_flock;
@@ -156,17 +155,12 @@ gcov_open (const char *name, int mode)
/* pass mode (ignored) for compatibility */
fd = open (name, O_RDONLY, S_IRUSR | S_IWUSR);
}
- else if (mode < 0)
+ else
{
/* Write mode - acquire a write-lock. */
s_flock.l_type = F_WRLCK;
- fd = open (name, O_RDWR | O_CREAT | O_TRUNC, 0666);
- }
- else /* mode == 0 */
- {
- /* Read-Write mode - acquire a write-lock. */
- s_flock.l_type = F_WRLCK;
- fd = open (name, O_RDWR | O_CREAT, 0666);
+ /* Truncate if force new mode. */
+ fd = open (name, O_RDWR | O_CREAT | (mode < 0 ? O_TRUNC : 0), 0666);
}
if (fd < 0)
return 0;
@@ -181,42 +175,23 @@ gcov_open (const char *name, int mode)
close (fd);
return 0;
}
-
- if (mode > 0)
- gcov_var.mode = 1;
- else if (mode == 0)
- {
- struct stat st;
-
- if (fstat (fd, &st) < 0)
- {
- fclose (gcov_var.file);
- gcov_var.file = 0;
- return 0;
- }
- if (st.st_size != 0)
- gcov_var.mode = 1;
- else
- gcov_var.mode = mode * 2 + 1;
- }
- else
- gcov_var.mode = mode * 2 + 1;
#else
if (mode >= 0)
+ /* Open an existing file. */
gcov_var.file = fopen (name, (mode > 0) ? "rb" : "r+b");
if (gcov_var.file)
- gcov_var.mode = 1;
+ mode = 1;
else if (mode <= 0)
- {
- gcov_var.file = fopen (name, "w+b");
- if (gcov_var.file)
- gcov_var.mode = mode * 2 + 1;
- }
+ /* Create a new file. */
+ gcov_var.file = fopen (name, "w+b");
+
if (!gcov_var.file)
return 0;
#endif
+ gcov_var.mode = mode ? mode : 1;
+
setbuf (gcov_var.file, (char *)0);
return 1;
@@ -372,8 +347,12 @@ gcov_write_string (const char *string)
buffer = gcov_write_words (1 + alloc);
buffer[0] = alloc;
- buffer[alloc] = 0;
- memcpy (&buffer[1], string, length);
+
+ if (alloc > 0)
+ {
+ buffer[alloc] = 0; /* place nul terminators. */
+ memcpy (&buffer[1], string, length);
+ }
}
#endif
@@ -446,13 +425,11 @@ gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
histo_bitvector[bv_ix] = 0;
csum = &summary->ctrs[GCOV_COUNTER_ARCS];
for (h_ix = 0; h_ix < GCOV_HISTOGRAM_SIZE; h_ix++)
- {
- if (csum->histogram[h_ix].num_counters > 0)
- {
- histo_bitvector[h_ix / 32] |= 1 << (h_ix % 32);
- h_cnt++;
- }
- }
+ if (csum->histogram[h_ix].num_counters)
+ {
+ histo_bitvector[h_ix / 32] |= 1 << (h_ix % 32);
+ h_cnt++;
+ }
gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH (h_cnt));
gcov_write_unsigned (summary->checksum);
for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
@@ -493,7 +470,9 @@ gcov_read_words (unsigned words)
const gcov_unsigned_t *result;
unsigned excess = gcov_var.length - gcov_var.offset;
- gcov_nonruntime_assert (gcov_var.mode > 0);
+ if (gcov_var.mode <= 0)
+ return NULL;
+
if (excess < words)
{
gcov_var.start += gcov_var.offset;