]> granicus.if.org Git - sysstat/commitdiff
tapestat: do a proper realloc
authorPeter Schiffer <pschiffe@redhat.com>
Wed, 25 May 2016 12:15:19 +0000 (14:15 +0200)
committerPeter Schiffer <pschiffe@redhat.com>
Wed, 25 May 2016 12:15:19 +0000 (14:15 +0200)
Store the output of a realloc in a temp var, so the original variable can be
freed upon failure.

tapestat.c

index 43b70f81b560a23b91b08799983d6decd3906860..534f3c3e48d9c0fc2ce985e6c44af51ee74cc3f5 100644 (file)
@@ -202,19 +202,24 @@ void tape_check_tapes_and_realloc(void)
 
        if (new_max_tape_drives > max_tape_drives) {
                /* New tapes found: Realloc structures */
-               tape_old_stats = (struct tape_stats *)
+               struct tape_stats *tape_old_stats_t = (struct tape_stats *)
                        realloc(tape_old_stats, sizeof(struct tape_stats) * new_max_tape_drives);
-               tape_new_stats=(struct tape_stats *)
+               struct tape_stats *tape_new_stats_t = (struct tape_stats *)
                        realloc(tape_new_stats, sizeof(struct tape_stats) * new_max_tape_drives);
-               if ((tape_old_stats == NULL) || (tape_new_stats == NULL)) {
-                       if (tape_old_stats != NULL) {
-                               free(tape_old_stats);
-                               tape_old_stats = NULL;
+               if ((tape_old_stats_t == NULL) || (tape_new_stats_t == NULL)) {
+                       if (tape_old_stats_t != NULL) {
+                               free(tape_old_stats_t);
+                               tape_old_stats_t = NULL;
                        }
-                       if (tape_new_stats != NULL) {
-                               free(tape_new_stats);
-                               tape_new_stats = NULL;
+                       if (tape_new_stats_t != NULL) {
+                               free(tape_new_stats_t);
+                               tape_new_stats_t = NULL;
                        }
+                       free(tape_old_stats);
+                       tape_old_stats = NULL;
+                       free(tape_new_stats);
+                       tape_new_stats = NULL;
+
                        perror("realloc");
                        exit(4);
                }