Skip to content

Commit 7889e9e

Browse files
committed
Fix double free in error case
Related to #153.
1 parent 1f5843f commit 7889e9e

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

src/statistics.zig

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn getReposByYear(
240240
"Getting {d} month{s} of data starting from {d}/{d}...",
241241
.{ months, if (months != 1) "s" else "", start_month + 1, year },
242242
);
243-
var response = try context.client.graphql(
243+
const response = try context.client.graphql(
244244
\\query ($from: DateTime, $to: DateTime) {
245245
\\ viewer {
246246
\\ contributionsCollection(from: $from, to: $to) {
@@ -289,7 +289,7 @@ fn getReposByYear(
289289
),
290290
},
291291
);
292-
errdefer context.client.allocator.free(response.body);
292+
defer context.client.allocator.free(response.body);
293293
if (response.status != .ok) {
294294
std.log.err(
295295
"Failed to get data from {d} ({?s})",
@@ -328,7 +328,6 @@ fn getReposByYear(
328328
response.body,
329329
.{ .ignore_unknown_fields = true, .allocate = .alloc_always },
330330
)).data.viewer.contributionsCollection;
331-
context.client.allocator.free(response.body);
332331
std.log.info(
333332
"Parsed {d} total repositories from {d}",
334333
.{ stats.commitContributionsByRepository.len, year },
@@ -426,7 +425,7 @@ fn getReposByYear(
426425
"Getting views for {s}...",
427426
.{raw_repo.nameWithOwner},
428427
);
429-
response = try context.client.rest(
428+
const response2 = try context.client.rest(
430429
try std.mem.concat(
431430
context.arena.allocator(),
432431
u8,
@@ -437,18 +436,18 @@ fn getReposByYear(
437436
},
438437
),
439438
);
440-
defer context.client.allocator.free(response.body);
441-
if (response.status == .ok) {
439+
defer context.client.allocator.free(response2.body);
440+
if (response2.status == .ok) {
442441
repository.views = (try std.json.parseFromSliceLeaky(
443442
struct { count: u32 },
444443
context.arena.allocator(),
445-
response.body,
444+
response2.body,
446445
.{ .ignore_unknown_fields = true },
447446
)).count;
448447
} else {
449448
std.log.info(
450449
"Failed to get views for {s} ({?s})",
451-
.{ raw_repo.nameWithOwner, response.status.phrase() },
450+
.{ raw_repo.nameWithOwner, response2.status.phrase() },
452451
);
453452
}
454453

0 commit comments

Comments
 (0)