fix: Plug memory leaks and fix strings that were not null terminated.

GreekStapler 2023-02-02 19:29:34 +00:00
parent b9dbb8af5c
commit e5f0ac8dec
2 changed files with 14 additions and 4 deletions

View File

@ -44,7 +44,7 @@ vieter_package *vieter_package_init() {
vieter_package_error vieter_package_read_archive(vieter_package *pkg, vieter_package_error vieter_package_read_archive(vieter_package *pkg,
const char *pkg_path) { const char *pkg_path) {
struct archive *a = archive_read_new(); struct archive *a = archive_read_new();
struct archive_entry *entry = archive_entry_new(); struct archive_entry *entry;
// These three are the most commonly used compression methods // These three are the most commonly used compression methods
archive_read_support_filter_zstd(a); archive_read_support_filter_zstd(a);
@ -88,8 +88,9 @@ vieter_package_error vieter_package_read_archive(vieter_package *pkg,
if (strcmp(path_name, ".PKGINFO") == 0) { if (strcmp(path_name, ".PKGINFO") == 0) {
// Read data of file into memory buffer // Read data of file into memory buffer
int size = archive_entry_size(entry); int size = archive_entry_size(entry);
char *buf = malloc(size); char *buf = malloc(size + 1);
archive_read_data(a, buf, size); archive_read_data(a, buf, size);
buf[size] = '\0';
// Parse package vieter_package_info string into a struct // Parse package vieter_package_info string into a struct
pkg_info = vieter_package_info_init(); pkg_info = vieter_package_info_init();
@ -248,6 +249,8 @@ char *vieter_package_to_description(vieter_package *pkg) {
strcat(description, "\n\n"); strcat(description, "\n\n");
free(aux);
return description; return description;
} }

View File

@ -8,8 +8,9 @@ void test_info_parse() {
size_t size = ftell(f); size_t size = ftell(f);
fflush(stdout); fflush(stdout);
rewind(f); rewind(f);
char *pkg_info_str = malloc(size); char *pkg_info_str = malloc(size + 1);
fread(pkg_info_str, 1, size, f); fread(pkg_info_str, 1, size, f);
pkg_info_str[size] = '\0';
fclose(f); fclose(f);
vieter_package_info *pkg_info = vieter_package_info_init(); vieter_package_info *pkg_info = vieter_package_info_init();
@ -36,6 +37,9 @@ void test_info_parse() {
TEST_CHECK(!strcmp(pkg_info->optdepends->array[0], "test7")); TEST_CHECK(!strcmp(pkg_info->optdepends->array[0], "test7"));
TEST_CHECK(!strcmp(pkg_info->makedepends->array[0], "xorg-xcursorgen")); TEST_CHECK(!strcmp(pkg_info->makedepends->array[0], "xorg-xcursorgen"));
TEST_CHECK(!strcmp(pkg_info->checkdepends->array[0], "test8")); TEST_CHECK(!strcmp(pkg_info->checkdepends->array[0], "test8"));
free(pkg_info_str);
vieter_package_info_free(pkg_info);
} }
void test_pkg_read_archive_files() { void test_pkg_read_archive_files() {
@ -73,13 +77,16 @@ void test_pkg_read_archive_desc() {
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
size_t size = ftell(f); size_t size = ftell(f);
rewind(f); rewind(f);
char *desc = malloc(size); char *desc = malloc(size + 1);
fread(desc, 1, size, f); fread(desc, 1, size, f);
desc[size] = '\0';
fclose(f); fclose(f);
TEST_CHECK(!strcmp(description, desc)); TEST_CHECK(!strcmp(description, desc));
vieter_package_free(&pkg); vieter_package_free(&pkg);
free(description);
free(desc);
} }
TEST_LIST = { TEST_LIST = {