Skip to content

Commit e867ee0

Browse files
committed
Merge pull request #136 from jen140/jen140-patch-1
Fixes for a few memory leaks.
2 parents 2424c6c + 2596f01 commit e867ee0

File tree

14 files changed

+41
-27
lines changed

14 files changed

+41
-27
lines changed

src/backend/backend.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,48 +89,48 @@ fs_backend *fs_backend_init(const char *db_name, int flags)
8989
ret->md = fs_metadata_open(db_name);
9090
if (!ret->md) {
9191
fs_error(LOG_CRIT, "cannot read metadata file for kb %s", db_name);
92-
92+
free(ret);
9393
return NULL;
9494
}
9595

9696
if (!fs_metadata_get_string(ret->md, FS_MD_NAME, NULL)) {
9797
fs_error(LOG_ERR, "no value for KB name in metadata, does KB exist?");
98-
98+
free(ret);
9999
return NULL;
100100
}
101101

102102
const char *hashfunc = fs_metadata_get_string(ret->md, FS_MD_HASHFUNC, "MD5");
103103
if (strcmp(hashfunc, FS_HASH)) {
104104
fs_error(LOG_ERR, "stored hash function does not match server's hash function");
105105
fs_error(LOG_ERR, "rebuild code with correct function or replace store");
106-
106+
free(ret);
107107
return NULL;
108108
}
109109

110110
const char *store_type = fs_metadata_get_string(ret->md, FS_MD_STORE, "semi-native");
111111
if (strcmp(store_type, "native")) {
112112
fs_error(LOG_ERR, "tried to open %s store with native backend", store_type);
113-
113+
free(ret);
114114
return NULL;
115115
}
116116

117117
if (strcmp(fs_metadata_get_string(ret->md, FS_MD_NAME, "-no-match-"), db_name)) {
118118
fs_error(LOG_CRIT, "metadata and opened KB name don't match %s / %s", db_name, fs_metadata_get_string(ret->md, FS_MD_NAME, "-no-match-"));
119-
119+
free(ret);
120120
return NULL;
121121
}
122122

123123
ret->segments = fs_metadata_get_int(ret->md, FS_MD_SEGMENTS, 0);
124124
const int version = fs_metadata_get_int(ret->md, FS_MD_VERSION, 0);
125125
if (version == -1) {
126126
fs_error(LOG_CRIT, "cannot find number of segments in KB %s", db_name);
127-
127+
free(ret);
128128
return NULL;
129129
}
130130
if (version > FS_CURRENT_TABLE_VERSION ||
131131
version < FS_EARLIEST_TABLE_VERSION) {
132132
fs_error(LOG_ERR, "wrong table metadata version in KB %s", db_name);
133-
133+
free(ret);
134134
return NULL;
135135
}
136136

src/backend/chain.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ fs_chain *fs_chain_open_filename(const char *fname, int flags)
103103
bc->fd = open(fname, FS_O_NOATIME | flags, FS_FILE_MODE);
104104
if (bc->fd == -1) {
105105
fs_error(LOG_CRIT, "failed to open chain %s: %s", fname, strerror(errno));
106-
106+
free(bc);
107107
return NULL;
108108
}
109109
bc->filename = g_strdup(fname);
@@ -117,7 +117,7 @@ fs_chain *fs_chain_open_filename(const char *fname, int flags)
117117
}
118118
if (header.id != CHAIN_ID) {
119119
fs_error(LOG_CRIT, "%s does not look like a chain file", bc->filename);
120-
120+
free(bc);
121121
return NULL;
122122
}
123123
if (map_bc(bc, header.length, header.size)) {

src/backend/list.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,23 @@ fs_list *fs_list_open_filename(const char *filename, size_t width, int flags)
8585
l->fd = open(filename, FS_O_NOATIME | flags, FS_FILE_MODE);
8686
if (l->fd == -1) {
8787
fs_error(LOG_ERR, "failed to open list file '%s': %s", l->filename, strerror(errno));
88-
88+
free(l);
8989
return NULL;
9090
}
9191
if ((flags & (O_WRONLY | O_RDWR)) && flock(l->fd, LOCK_EX) == -1) {
9292
fs_error(LOG_ERR, "failed to open list: %s, cannot get lock: %s", filename, strerror(errno));
93-
93+
free(l);
9494
return NULL;
9595
}
9696
off_t end = lseek(l->fd, 0, SEEK_END);
9797
if (end == -1) {
9898
fs_error(LOG_CRIT, "failed to open list: %s, cannot seek to end", filename);
99-
99+
free(l);
100100
return NULL;
101101
}
102102
if (end % width != 0) {
103103
fs_error(LOG_CRIT, "failed to open list: %s, length not multiple of data size", filename);
104-
104+
free(l);
105105
return NULL;
106106
}
107107
l->offset = end / width;
@@ -111,7 +111,7 @@ fs_list *fs_list_open_filename(const char *filename, size_t width, int flags)
111111
if (l->fd == -1) {
112112
fs_error(LOG_CRIT, "failed to open list %s: %s", filename,
113113
strerror(errno));
114-
114+
free(l);
115115
return NULL;
116116
}
117117

src/backend/metadata.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ fs_metadata *fs_metadata_open(const char *kb)
7777
if ((fd = open(m->uri + 7, FS_O_NOATIME | O_CREAT, FS_FILE_MODE)) == -1) {
7878
fs_error(LOG_CRIT, "failed to touch metadata file %s: %s",
7979
m->uri, strerror(errno));
80-
80+
free(m);
8181
return NULL;
8282
}
8383
close(fd);
8484
m->rw = raptor_new_world();
8585
if (!m->rw) {
8686
fs_error(LOG_CRIT, "failed to initialise raptor");
87-
87+
free(m);
8888
return NULL;
8989
}
9090
raptor_parser *rdf_parser = raptor_new_parser(m->rw, "turtle");

src/backend/mhash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fs_mhash *fs_mhash_open_filename(const char *filename, int flags)
100100
mh->flags = flags;
101101
if (mh->fd == -1) {
102102
fs_error(LOG_ERR, "cannot open mhash file '%s': %s", filename, strerror(errno));
103-
103+
free(mh);
104104
return NULL;
105105
}
106106
mh->filename = g_strdup(filename);

src/backend/ptable.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,13 @@ fs_ptable *fs_ptable_open_filename(const char *fname, int flags)
134134

135135
if (sizeof(struct ptable_header) != 512) {
136136
fs_error(LOG_CRIT, "ptable header size not 512 bytes");
137+
free(pt);
137138
return NULL;
138139
}
139140
pt->fd = open(fname, FS_O_NOATIME | flags, FS_FILE_MODE);
140141
if (pt->fd == -1) {
141142
fs_error(LOG_CRIT, "failed to open ptable %s: %s", fname, strerror(errno));
143+
free(pt);
142144
return NULL;
143145
}
144146
pt->filename = g_strdup(fname);

src/backend/rhash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fs_rhash *fs_rhash_open_filename(const char *filename, int flags)
169169
rh->flags = flags;
170170
if (rh->fd == -1) {
171171
fs_error(LOG_ERR, "cannot open rhash file '%s': %s", filename, strerror(errno));
172-
172+
free(rh);
173173
return NULL;
174174
}
175175
rh->z_buffer_size = 1024;

src/backend/tbchain.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,27 @@ fs_tbchain *fs_tbchain_open_filename(const char *fname, int flags)
165165
bc->fd = open(fname, FS_O_NOATIME | flags, FS_FILE_MODE);
166166
if (bc->fd == -1) {
167167
fs_error(LOG_CRIT, "failed to open chain %s: %s", fname, strerror(errno));
168+
free(bc);
168169
return NULL;
169170
}
170171
bc->filename = g_strdup(fname);
171172
bc->flags = flags;
172173

173174
if (sizeof(*header) != 512) {
174175
fs_error(LOG_CRIT, "tbchain header size is %zd, not 512 bytes", sizeof(header));
176+
free(bc);
175177
return NULL;
176178
}
177179
if (sizeof(fs_tblock) != FS_TBLOCK_SIZE) {
178180
fs_error(LOG_CRIT, "tblock size is %zd, not %d bytes", sizeof(fs_tblock), FS_TBLOCK_SIZE);
181+
free(bc);
179182
return NULL;
180183
}
181184
int init_reqd = 0;
182185

183186
if (bc->flags & O_TRUNC && ftruncate(bc->fd, sizeof(*header))) {
184187
fs_error(LOG_CRIT, "ftruncate failed on %s: %s", fname, strerror(errno));
188+
free(bc);
185189
return NULL;
186190
}
187191

@@ -192,6 +196,7 @@ fs_tbchain *fs_tbchain_open_filename(const char *fname, int flags)
192196
if (header == MAP_FAILED || header == NULL) {
193197
munmap(header, sizeof(*header));
194198
fs_error(LOG_CRIT, "header mmap failed for %s: %s", bc->filename, strerror(errno));
199+
free(bc);
195200
return NULL;
196201
}
197202

src/backend/tlist.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fs_tlist *fs_tlist_open_filename(const char *filename, int flags)
130130
if (length == HEADER && header.id != TLIST_ID) {
131131
fs_error(LOG_ERR, "“%s” does not appear to be a tlist", filename);
132132
close(l->fd);
133-
133+
free(l);
134134
return NULL;
135135
}
136136
l->offset = header.length;
@@ -141,7 +141,7 @@ fs_tlist *fs_tlist_open_filename(const char *filename, int flags)
141141
if ((flags & (O_WRONLY | O_RDWR)) && flock(l->fd, LOCK_EX) == -1) {
142142
fs_error(LOG_ERR, "failed to open list: %s, cannot get lock: %s", filename, strerror(errno));
143143
close(l->fd);
144-
144+
free(l);
145145
return NULL;
146146
}
147147
write_header(l);
@@ -150,7 +150,7 @@ fs_tlist *fs_tlist_open_filename(const char *filename, int flags)
150150
if (l->fd == -1) {
151151
fs_error(LOG_CRIT, "failed to open list %s: %s", filename,
152152
strerror(errno));
153-
153+
free(l);
154154
return NULL;
155155
}
156156

src/backend/tree.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ fs_tree *fs_tree_open_filename(fs_backend *be, const char *name, const char *fil
192192
int fd = open(filename, FS_O_NOATIME | O_RDWR | flags, FS_FILE_MODE);
193193
if (fd == -1) {
194194
fs_error(LOG_ERR, "failed to open tree file '%s': %s", filename, strerror(errno));
195+
free(t);
195196
return NULL;
196197
}
197198
t->filename = (char *)filename;
@@ -214,13 +215,13 @@ fs_tree *fs_tree_open_filename(fs_backend *be, const char *name, const char *fil
214215
if (header.id != TREE_ID) {
215216
fs_error(LOG_ERR, "'%s' does not appear to be a valid treefile", filename);
216217
close(fd);
217-
218+
free(t);
218219
return NULL;
219220
}
220221
t->bc = fs_chain_open_filename(header.chainfile, flags);
221222
if (!t->bc) {
222223
fs_error(LOG_CRIT, "failed to open chain file '%s'", header.chainfile);
223-
224+
free(t);
224225
return NULL;
225226
}
226227

0 commit comments

Comments
 (0)