Commit 8ba28587 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov
Browse files

selftests/bpf: Fix memory leaks in btf_type_c_dump() helper



Free up memory and resources used by temporary allocated memstream and
btf_dump instance.

Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Reviewed-by: default avatarHengqi Chen <hengqi.chen@gmail.com>
Link: https://lore.kernel.org/bpf/20211107165521.9240-4-andrii@kernel.org
parent 8f7b239e
Loading
Loading
Loading
Loading
+7 −2
Original line number Original line Diff line number Diff line
@@ -251,18 +251,23 @@ const char *btf_type_c_dump(const struct btf *btf)
	d = btf_dump__new(btf, NULL, &opts, btf_dump_printf);
	d = btf_dump__new(btf, NULL, &opts, btf_dump_printf);
	if (libbpf_get_error(d)) {
	if (libbpf_get_error(d)) {
		fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d));
		fprintf(stderr, "Failed to create btf_dump instance: %ld\n", libbpf_get_error(d));
		return NULL;
		goto err_out;
	}
	}


	for (i = 1; i < btf__type_cnt(btf); i++) {
	for (i = 1; i < btf__type_cnt(btf); i++) {
		err = btf_dump__dump_type(d, i);
		err = btf_dump__dump_type(d, i);
		if (err) {
		if (err) {
			fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
			fprintf(stderr, "Failed to dump type [%d]: %d\n", i, err);
			return NULL;
			goto err_out;
		}
		}
	}
	}


	btf_dump__free(d);
	fflush(buf_file);
	fflush(buf_file);
	fclose(buf_file);
	fclose(buf_file);
	return buf;
	return buf;
err_out:
	btf_dump__free(d);
	fclose(buf_file);
	return NULL;
}
}