]> granicus.if.org Git - sysstat/commitdiff
sar/sadf: Test values returned by functions
authorSebastien GODARD <sysstat@users.noreply.github.com>
Sat, 6 Feb 2021 17:09:46 +0000 (18:09 +0100)
committerSebastien GODARD <sysstat@users.noreply.github.com>
Sat, 6 Feb 2021 17:09:46 +0000 (18:09 +0100)
Signed-off-by: Sebastien GODARD <sysstat@users.noreply.github.com>
sa.h
sa_common.c
sadf.c
sar.c
svg_stats.c

diff --git a/sa.h b/sa.h
index 610797186340478c030c8ad457cfda2138d73b5f..9029a938b07db99c4e5c21454b911963b3b2fb9b 100644 (file)
--- a/sa.h
+++ b/sa.h
@@ -1486,7 +1486,7 @@ void handle_invalid_sa_file
        (int, struct file_magic *, char *, int);
 void print_collect_error
        (void);
-int set_default_file
+void set_default_file
        (char *, int, int);
 int skip_extra_struct
        (int, int, int);
index 8d1eb17b0294fe9a5a9c4b5a9e15693912d409cb..27aa4cb2d4e0541d4b585ab25c3a68994c77ea66 100644 (file)
@@ -198,13 +198,9 @@ void guess_sa_name(char *sa_dir, struct tm *rectime, int *sa_name)
  *
  * OUT:
  * @datafile   Name of daily data file.
- *
- * RETURNS:
- * 1 if an output error has been encountered or if datafile name has been
- * truncated, or 0 otherwise.
  ***************************************************************************
  */
-int set_default_file(char *datafile, int d_off, int sa_name)
+void set_default_file(char *datafile, int d_off, int sa_name)
 {
        char sa_dir[MAX_FILE_LEN];
        struct tm rectime = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
@@ -243,13 +239,17 @@ int set_default_file(char *datafile, int d_off, int sa_name)
                               rectime.tm_mday);
        }
        datafile[MAX_FILE_LEN - 1] = '\0';
+
+       if ((err < 0) || (err >= MAX_FILE_LEN)) {
+               fprintf(stderr, "%s: %s\n", __FUNCTION__, datafile);
+               exit(1);
+       }
+
        default_file_used = TRUE;
 
 #ifdef DEBUG
        fprintf(stderr, "%s: Datafile: %s\n", __FUNCTION__, datafile);
 #endif
-
-       return ((err < 0) || (err >= MAX_FILE_LEN));
 }
 
 /*
diff --git a/sadf.c b/sadf.c
index 4982197a4bd0552076eee879eb0723a2fa99cd8a..c648692d34a8fbf4242d9d4dc10dda7c692606de 100644 (file)
--- a/sadf.c
+++ b/sadf.c
@@ -230,7 +230,7 @@ void check_format_options(void)
  *
  * RETURNS:
  * 1 if EOF has been reached,
- * 2 if an unexpected EOF has been reached,
+ * 2 if an unexpected EOF has been reached, or an error occurred.
  * 0 otherwise.
  ***************************************************************************
  */
@@ -265,8 +265,9 @@ int read_next_sample(int ifd, int action, int curr, char *file, int *rtype, int
                                return 2;
 
                        if (action & SET_TIMESTAMPS) {
-                               sa_get_record_timestamp_struct(flags, &record_hdr[curr],
-                                                              rectime);
+                               if (sa_get_record_timestamp_struct(flags, &record_hdr[curr],
+                                                                  rectime))
+                                       return 2;
                        }
                }
                else {
@@ -293,7 +294,8 @@ int read_next_sample(int ifd, int action, int curr, char *file, int *rtype, int
                                        return 2;
                        }
                        if (action & SET_TIMESTAMPS) {
-                               sa_get_record_timestamp_struct(flags, &record_hdr[curr], rectime);
+                               if (sa_get_record_timestamp_struct(flags, &record_hdr[curr], rectime))
+                                       return 2;
                        }
                }
                else {
@@ -312,7 +314,8 @@ int read_next_sample(int ifd, int action, int curr, char *file, int *rtype, int
                if (read_file_stat_bunch(act, curr, ifd, file_hdr.sa_act_nr, file_actlst,
                                         endian_mismatch, arch_64, file, file_magic, oneof) > 0)
                        return 2;
-               sa_get_record_timestamp_struct(flags, &record_hdr[curr], rectime);
+               if (sa_get_record_timestamp_struct(flags, &record_hdr[curr], rectime))
+                       return 2;
        }
 
        return 0;
diff --git a/sar.c b/sar.c
index 490016b3d6e036bf6b7dc919466cbe44d9745ae5..519ad65bbc7ab743aa8bf7db2ed0bc3364e614bf 100644 (file)
--- a/sar.c
+++ b/sar.c
@@ -800,8 +800,10 @@ void handle_curr_act_stats(int ifd, off_t fpos, int *curr, long *cnt, int *eosaf
 
                if (rtype != R_COMMENT) {
                        /* Read the extra fields since it's not a special record */
-                       read_file_stat_bunch(act, *curr, ifd, file_hdr.sa_act_nr, file_actlst,
-                                            endian_mismatch, arch_64, file, file_magic, UEOF_STOP);
+                       if (read_file_stat_bunch(act, *curr, ifd, file_hdr.sa_act_nr, file_actlst,
+                                                endian_mismatch, arch_64, file, file_magic, UEOF_STOP))
+                               /* Error or unexpected EOF */
+                               break;
                }
                else {
                        /* Display comment */
@@ -1045,9 +1047,12 @@ void read_stats_from_file(char from_file[])
                                 * OK: Previous record was not a special one.
                                 * So read now the extra fields.
                                 */
-                               read_file_stat_bunch(act, 0, ifd, file_hdr.sa_act_nr,
-                                                    file_actlst, endian_mismatch, arch_64,
-                                                    from_file, &file_magic, UEOF_STOP);
+                               if (read_file_stat_bunch(act, 0, ifd, file_hdr.sa_act_nr,
+                                                        file_actlst, endian_mismatch, arch_64,
+                                                        from_file, &file_magic, UEOF_STOP))
+                                       /* Possible unexpected EOF */
+                                       return;
+
                                if (sa_get_record_timestamp_struct(flags + S_F_LOCAL_TIME,
                                                                   &record_hdr[0], &rectime))
                                        /*
@@ -1133,9 +1138,11 @@ void read_stats_from_file(char from_file[])
                                        break;
 
                                if (rtype != R_COMMENT) {
-                                       read_file_stat_bunch(act, curr, ifd, file_hdr.sa_act_nr,
-                                                            file_actlst, endian_mismatch, arch_64,
-                                                            from_file, &file_magic, UEOF_STOP);
+                                       if (read_file_stat_bunch(act, curr, ifd, file_hdr.sa_act_nr,
+                                                                file_actlst, endian_mismatch, arch_64,
+                                                                from_file, &file_magic, UEOF_STOP))
+                                               /* Possible unexpected EOF */
+                                               break;
                                }
                                else {
                                        /* This was a COMMENT record: Print it */
index b0ab564a79b3e0d77ed3dc3f5b57a6b16426ad78..5427458cf22ec5a9b29b0f23806e04e4c5fc956c 100644 (file)
@@ -670,7 +670,12 @@ void display_vgrid(long int xpos, double xfactor, int v_gridnr, struct svg_parm
        for (j = 0; (j <= (2 * v_gridnr)) && (stamp.ust_time <= svg_p->ust_time_end); j++) {
 
                /* Display vertical lines */
-               sa_get_record_timestamp_struct(flags, &stamp, &rectime);
+               if (sa_get_record_timestamp_struct(flags, &stamp, &rectime)) {
+#ifdef DEBUG
+                       fprintf(stderr, "%s: ust_time: %llu\n", __FUNCTION__, stamp.ust_time);
+#endif
+                       exit(1);
+               }
                set_record_timestamp_string(flags, &stamp, NULL, cur_time, TIMESTAMP_LEN, &rectime);
                printf("<polyline points=\"%ld,0 %ld,%d\" style=\"vector-effect: non-scaling-stroke; "
                       "stroke: #%06x\" transform=\"scale(%f,1)\"/>\n",