Skip to content

Commit 9930ed9

Browse files
committed
better checking of malloc
1 parent 2a0e30c commit 9930ed9

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

src/diff.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
#include "fs.h"
33
#include <stdlib.h>
44
#include <string.h>
5+
#include <stdio.h>
56

67
char **missing_from_db(char **dbList, struct fs_discovered_migrations *fsList) {
78
int fsi = 0;
89
char **list = malloc(1000 * sizeof(char *));
10+
if (list == NULL) {
11+
printf("malloc failed to allocate\n");
12+
exit(1);
13+
}
914
int filesLocated = 0;
1015

1116
while (strcmp(fsList[fsi].name, "\0")) {
@@ -20,12 +25,20 @@ char **missing_from_db(char **dbList, struct fs_discovered_migrations *fsList) {
2025
}
2126
if (!located) {
2227
list[filesLocated] = (char *) malloc(PATH_MAX + 1);
28+
if (list[filesLocated] == NULL) {
29+
printf("malloc failed to allocate\n");
30+
exit(1);
31+
}
2332
strcpy(list[filesLocated], fsList[fsi].name);
2433
filesLocated++;
2534
}
2635
fsi++;
2736
}
2837
list[filesLocated] = (char *)malloc(PATH_MAX + 1);
38+
if (list[filesLocated] == NULL) {
39+
printf("malloc failed to allocate\n");
40+
exit(1);
41+
}
2942
strcpy(list[filesLocated],"\0");
3043
return list;
3144

src/fs.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ void listdir(const char *name, int level) {
5858

5959
struct fs_discovered_migrations *getMigrationsFromFs(const char *dir) {
6060
file_names_arr = calloc(1000, sizeof(*file_names_arr));
61+
if (file_names_arr == NULL) {
62+
printf("failed to allocate memory\n");
63+
exit(1);
64+
}
6165
itr = calloc(1000, sizeof(int));
66+
if (itr == NULL) {
67+
printf("failed to allocate memory\n");
68+
exit(1);
69+
}
6270
*itr = 0;
6371
listdir(".", 15);
6472
strcpy(file_names_arr[*itr].name, "\0");

src/main.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "diff.h"
99

1010
void printHelp();
11-
const char* version = "1.0.0";
11+
const char *version = "1.0.0";
1212

1313
int main(int argc, char *argv[]) {
1414
extern char *optarg;
@@ -18,10 +18,14 @@ int main(int argc, char *argv[]) {
1818
* s-status, H-Host, u-up, d-down, v-version, p-soft, g-provision, h-help
1919
*/
2020
int c = 0, u = 0, d = 0, s = 0, g = 0, p = 0, H = 0;
21-
char *connStr = (char*)malloc(PATH_MAX*sizeof(connStr));
21+
char *connStr = (char *) malloc(PATH_MAX * sizeof(connStr));
22+
if (connStr == NULL) {
23+
printf("malloc failed to allocate\n");
24+
exit(1);
25+
}
2226

23-
if( connStr == NULL ) {
24-
printf("malloc failed to dimension connStr");
27+
if (connStr == NULL) {
28+
printf("malloc failed to dimension connStr\n");
2529
exit(1);
2630
}
2731

@@ -123,10 +127,14 @@ int main(int argc, char *argv[]) {
123127
}
124128

125129
if (u) {
130+
if (argv[optind] == NULL) {
131+
fprintf(stderr, "No directory provided\n");
132+
exit(1);
133+
}
126134
char *file = argv[optind];
127135
char path[PATH_MAX];
128136
realpath(file, path);
129-
DIR* dir = opendir(path);
137+
DIR *dir = opendir(path);
130138
if (!dir) {
131139
fprintf(stderr, "No valid directory provided %s\n", path);
132140
exit(1);

src/pg.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,26 @@ char **getMigrationsFromDb(PGconn *connection) {
9595
char **list = malloc(1000 * sizeof(char *));
9696
if (list == NULL) {
9797
PQfinish(connection);
98+
printf("malloc failed\n");
9899
exit(1);
99100
}
100101

101102
int i = 0;
102103
for (; i < rows; i++) {
103104
list[i] = (char *) malloc(PATH_MAX + 1);
105+
if (list[i] == NULL) {
106+
PQfinish(connection);
107+
printf("malloc failed\n");
108+
exit(1);
109+
}
104110
strcpy(list[i], strdup(PQgetvalue(res, i, 0)));
105111
}
106112
list[i + 1] = (char *) malloc(PATH_MAX + 1);
113+
if (list[i + 1] == NULL) {
114+
PQfinish(connection);
115+
printf("malloc failed\n");
116+
exit(1);
117+
}
107118
strcpy(list[i + 1], "\0");
108119

109120
PQclear(res);
@@ -143,6 +154,10 @@ void runMigrations(PGconn *connection, char **migrationsToBeRan, int should_simu
143154
}
144155

145156
char *fileContents = malloc(fsize + 1);
157+
if (fileContents == NULL) {
158+
printf("malloc failed to allocate\n");
159+
exit(1);
160+
}
146161
fread(fileContents, fsize, 1, f);
147162
fclose(f);
148163

@@ -236,6 +251,10 @@ void rollbackMigrations(PGconn *connection, int should_simulate) {
236251

237252
if (should_simulate == 0) {
238253
char *fileContents = malloc(fsize + 1);
254+
if (fileContents == NULL) {
255+
printf("malloc failed to allocate\n");
256+
exit(1);
257+
}
239258
fread(fileContents, fsize, 1, f);
240259
fclose(f);
241260

0 commit comments

Comments
 (0)