Skip to content

Commit a84dec3

Browse files
free guards
1 parent e527642 commit a84dec3

File tree

13 files changed

+77
-10
lines changed

13 files changed

+77
-10
lines changed

src/fs/fat32/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ int read_fat(fat32_t* info)
6060
info->numberoffats = par->numberoffats;
6161
info->fatsize = par->sectorsperfat;
6262
info->info = kmalloc(sizeof(fat32_fs_info_t));
63+
if (!info->info) {
64+
kfree_null(&buffer);
65+
return 0;
66+
}
6367
info->clustersize = par->sectorspercluster;
6468
info->clustersize *= sd->block_size;
6569

6670
if (info->clustersize == 0) {
71+
kfree_null(&buffer);
6772
fs_set_error(FS_ERR_IS_EXFAT);
6873
return 0;
6974
}

src/fs/filesystem.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ bool vfs_path_add_child(const char* path, const char* child) {
178178
}
179179
if (exists->child_count == 0) {
180180
exists->children = kmalloc(sizeof(char*));
181+
if (!exists->children) {
182+
return false;
183+
}
181184
exists->children[0] = strdup(child);
182185
exists->child_count = 1;
183186
} else {
@@ -196,6 +199,9 @@ bool vfs_path_add_child(const char* path, const char* child) {
196199
}
197200
exists->child_count++;
198201
exists->children[exists->child_count - 1] = strdup(child);
202+
if (!exists->children[exists->child_count - 1]) {
203+
return false;
204+
}
199205
}
200206
return true;
201207
}
@@ -662,6 +668,9 @@ fs_directory_entry_t* fs_create_directory(const char* pathandfile)
662668
if (lbapos) {
663669
dprintf("Driver createdirectory %lu\n", lbapos);
664670
new_entry = kmalloc(sizeof(fs_directory_entry_t));
671+
if (!new_entry) {
672+
return NULL;
673+
}
665674
datetime_t dt;
666675
get_datetime(&dt);
667676
get_weekday_from_date(&dt);
@@ -683,6 +692,10 @@ fs_directory_entry_t* fs_create_directory(const char* pathandfile)
683692
directory->files = new_entry;
684693
/* TODO add to FS tree, dont forget responsible_driver ptr! */
685694
fs_tree_t* new_dir = kmalloc(sizeof(fs_tree_t));
695+
if (!new_dir) {
696+
kfree_null(&new_entry);
697+
return NULL;
698+
}
686699
new_dir->next = directory->child_dirs;
687700
new_dir->device = directory->device;
688701
strlcpy(new_dir->device_name, directory->device_name, 16);

src/fs/iso9660.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ int parse_pvd(iso9660 *info, unsigned char *buffer) {
4040

4141
size_t j = 0;
4242
info->volume_name = kmalloc(strlen(pvd->volumeidentifier) + 1);
43+
if (!info->volume_name) {
44+
return false;
45+
}
4346
for (ptr = pvd->volumeidentifier; *ptr && j < strlen(pvd->volumeidentifier); ++ptr) {
4447
info->volume_name[j++] = *ptr;
4548
}
@@ -363,6 +366,9 @@ int iso9660_attach(const char *device, const char *path) {
363366

364367
void init_iso9660() {
365368
iso9660_fs = kmalloc(sizeof(filesystem_t));
369+
if (!iso9660_fs) {
370+
return;
371+
}
366372
strlcpy(iso9660_fs->name, "iso9660", 31);
367373
iso9660_fs->mount = iso9660_attach;
368374
iso9660_fs->getdir = iso_get_directory;

src/fs/retrofs/getdirectory.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ fs_directory_entry_t *rfs_walk_directory(fs_tree_t *tree, rfs_t *info, uint64_t
4343
}
4444
/* Process entry */
4545
fs_directory_entry_t *file = kmalloc(sizeof(fs_directory_entry_t));
46+
if (!file) {
47+
return NULL;
48+
}
4649
file->filename = strdup(entry->filename);
4750
file->alt_filename = strdup(entry->filename);
4851
file->lbapos = entry->sector_start;

src/installer/gpt_writer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ bool prepare_rfs_partition(storage_device_t* dev) {
218218
uint8_t partitionid = 0;
219219
char found_guid[64];
220220
rfs_t *info = kmalloc(sizeof(rfs_t));
221+
if (!info) {
222+
return false;
223+
}
221224
memset(info, 0, sizeof(rfs_t));
222225
uint64_t start = 0, length = 0;
223226
info->dev = dev;

src/installer/recursive_file_copy.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ bool copy_file(const char* source, const char* destination) {
1212
fs_directory_entry_t* info = fs_get_file_info(source);
1313
if (info && !(info->flags & FS_DIRECTORY) && info->size > 0) {
1414
void *file_contents = kmalloc(info->size);
15+
if (!file_contents) {
16+
return false;
17+
}
1518
bool read_success = fs_read_file(info, 0, info->size, file_contents);
1619
if (!read_success) {
1720
error_page("Error reading '%s' (%s)\n", source, fs_strerror(fs_get_error()));

src/net/ip.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,10 @@ void ip_handle_packet(ip_packet_t* packet, [[maybe_unused]] int n_len) {
538538
/* We have complete packet, all fragments - reassemble the data part and free everything */
539539
ip_packet_frag_t* cur = fragmented->ordered_list;
540540
data_ptr = kmalloc(fragmented->size);
541+
if (!data_ptr) {
542+
dprintf("Can't allocate memory for frag\n");
543+
return;
544+
}
541545
data_len = fragmented->size;
542546
/* Set flag to indicate we need to free the data_ptr later */
543547
fragment_to_free = true;

src/net/queue.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@
22

33
inline queue_t* queue_new(void) {
44
queue_t *q = kmalloc(sizeof(queue_t));
5-
if (!q) return NULL;
5+
if (!q) {
6+
return NULL;
7+
}
68
q->head = q->tail = NULL;
79
return q;
810
}
911

1012
inline void queue_push(queue_t *q, tcp_conn_t *conn) {
1113
pending_node_t *n = kmalloc(sizeof(pending_node_t));
12-
if (!n) return;
14+
if (!n) {
15+
return;
16+
}
1317
n->conn = conn;
1418
n->next = NULL;
15-
if (q->tail) q->tail->next = n;
16-
else q->head = n;
19+
if (q->tail) {
20+
q->tail->next = n;
21+
} else {
22+
q->head = n;
23+
}
1724
q->tail = n;
1825
}
1926

2027
inline tcp_conn_t* queue_pop(queue_t *q) {
21-
if (!q || !q->head) return NULL;
28+
if (!q || !q->head) {
29+
return NULL;
30+
}
2231
pending_node_t *n = q->head;
2332
tcp_conn_t *c = n->conn;
2433
q->head = n->next;
25-
if (!q->head) q->tail = NULL;
34+
if (!q->head) {
35+
q->tail = NULL;
36+
}
2637
kfree_null(&n);
2738
return c;
2839
}

src/net/tcp.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ tcp_conn_t* tcp_send_segment(tcp_conn_t *conn, uint32_t seq, uint8_t flags, cons
291291
tcp_options_t options = { .mss = flags & TCP_SYN ? 1460 : 0 };
292292
uint16_t length = sizeof(tcp_segment_t) + count + (flags & TCP_SYN ? 4 : 0);
293293
tcp_segment_t* packet = kmalloc(length);
294+
if (!packet) {
295+
return NULL;
296+
}
294297

295298
memset(packet, 0, length);
296299
packet->src_port = conn->local_port;
@@ -441,7 +444,14 @@ tcp_segment_t* tcp_ord_list_insert(tcp_conn_t* conn, tcp_segment_t* segment, siz
441444

442445
// allocate new node
443446
tcp_ordered_list_t *new = kmalloc(sizeof(tcp_ordered_list_t));
447+
if (!new) {
448+
return NULL;
449+
}
444450
new->segment = kmalloc(len + tcp_header_size(segment));
451+
if (!new->segment) {
452+
kfree_null(&new);
453+
return NULL;
454+
}
445455
memcpy(new->segment, segment, len + tcp_header_size(segment));
446456
new->len = len;
447457
new->prev = NULL;

src/rtl8139.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,6 @@ void init_rtl8139() {
306306

307307
rtl_outl(RxBuf, rtl8139_device.rx_buffer);
308308

309-
kprintf("RX buffer assigned at %08x\n", rtl8139_device.rx_buffer);
310-
311309
rtl_outb(RxEarlyThresh, 0x3C);
312310

313311
for(int i=0; i < 4; i++) {
@@ -341,6 +339,9 @@ void init_rtl8139() {
341339
make_unique_device_name("net", rtl8139_device.name, sizeof(rtl8139_device.name));
342340

343341
netdev_t* net = kmalloc(sizeof(netdev_t));
342+
if (!net) {
343+
return;
344+
}
344345
net->opaque = &rtl8139_device;
345346
net->deviceid = (RTL8139_VENDOR_ID << 16) | RTL8139_DEVICE_ID;
346347
strlcpy(net->name, rtl8139_device.name, 16);

0 commit comments

Comments
 (0)