Skip to content

Commit 7a3d4ee

Browse files
committed
feat: Added checkups for correct memory allocation on all strdup (malloc)
1 parent d8c223b commit 7a3d4ee

File tree

22 files changed

+236
-37
lines changed

22 files changed

+236
-37
lines changed

src/libltfs/arch/filename_handling.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ void update_platform_safe_name(struct dentry* dentry, bool handle_invalid_char,
130130
}
131131
}
132132
#else
133+
// Checkup not needed here, memory error handling happens after function calls.
133134
dentry->platform_safe_name = strdup(dentry->name.name);
134135
#endif
135136
}
@@ -242,6 +243,10 @@ char * _generate_target_file_name(const char *prefix, const char *extension, int
242243
ret = asprintf(&target, "%s.%s", prefix, extension);
243244
else {
244245
target = strdup(prefix);
246+
if (!target) {
247+
ltfsmsg(LTFS_ERR, 10001E, "_generate_target_file_name: target assign");
248+
ret = -1;
249+
}
245250
ret = target ? strlen(target) : -1;
246251
}
247252
}

src/libltfs/fs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ static char* generate_hash_key_name(const char *src_str, int *rc)
8989
} else
9090
free(uchar_name);
9191
#else
92+
// Chek not needed here for strdup because the error is handled where the function is called
9293
key_name = strdup(src_str);
9394
*rc = 0;
9495
#endif

src/libltfs/index_criteria.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -288,27 +288,30 @@ int index_criteria_parse_name(const char *criteria, size_t len, struct index_cri
288288
/* Assign rules to the glob_patterns[] array */
289289
rule = rule+5;
290290
for (delim = rule; *delim; delim++) {
291-
if (*delim == ':') {
292-
*delim = '\0';
293-
rule_ptr->percent_encode = fs_is_percent_encode_required(rule);
294-
rule_ptr->name = strdup(rule);
295-
rule_ptr++;
296-
rule = delim+1;
297-
} else if (*delim == '/') {
298-
*delim = '\0';
299-
rule_ptr->percent_encode = fs_is_percent_encode_required(rule);
300-
rule_ptr->name = strdup(rule);
301-
rule_ptr++;
302-
} else if (*(delim+1) == '\0') {
291+
if (*delim == ':' || *delim == '/') {
292+
*delim = '\0';
293+
}
303294
rule_ptr->percent_encode = fs_is_percent_encode_required(rule);
304295
rule_ptr->name = strdup(rule);
296+
if (! rule_ptr->name) {
297+
ltfsmsg(LTFS_ERR, 10001E, "index_criteria_parse_name: rule assign");
298+
free(rule_ptr->name);
299+
return -EDEV_NO_MEMORY;
300+
}
305301
rule_ptr++;
306-
}
302+
if (*delim == ':') {
303+
rule = delim+1;
304+
}
307305
}
308306

309307
if (ic->glob_patterns == rule_ptr) {
310308
rule_ptr->percent_encode = fs_is_percent_encode_required(rule);
311309
rule_ptr->name = strdup(rule);
310+
if (! rule_ptr->name) {
311+
ltfsmsg(LTFS_ERR, 10001E, "index_criteria_parse_name: glob_patterns assign");
312+
free(rule_ptr->name);
313+
return -EDEV_NO_MEMORY;
314+
}
312315
}
313316

314317
/* Validate rules */

src/libltfs/ltfs.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
** OO_Copyright_BEGIN
44
**
55
**
6-
** Copyright 2010, 2025 IBM Corp. All rights reserved.
6+
** Copyright 2010, 2020 IBM Corp. All rights reserved.
77
**
88
** Redistribution and use in source and binary forms, with or without
99
** modification, are permitted provided that the following conditions
@@ -1658,8 +1658,7 @@ int ltfs_mount(bool force_full, bool deep_recovery, bool recover_extra, bool rec
16581658
(unsigned long long)vol->dp_coh.volume_change_ref,
16591659
(unsigned long long)volume_change_ref);
16601660

1661-
/* Index of IP could be corrupted. So set skip flag to true */
1662-
ret = _ltfs_search_index_wp(recover_symlink, true, &seekpos, vol);
1661+
ret = _ltfs_search_index_wp(recover_symlink, false, &seekpos, vol);
16631662
if (ret < 0)
16641663
goto out_unlock;
16651664

@@ -1669,7 +1668,7 @@ int ltfs_mount(bool force_full, bool deep_recovery, bool recover_extra, bool rec
16691668
seekpos.block = vol->dp_coh.set_id;
16701669
}
16711670
} else {
1672-
if (vol->ip_coh.count > vol->dp_coh.count && vollock != PWE_MAM_DP && vollock != PWE_MAM) {
1671+
if (vollock != PWE_MAM_DP && vollock != PWE_MAM) {
16731672
/*
16741673
* The index on IP is newer but MAM shows write perm doesn't happen in DP.
16751674
* LTFS already have written an index on DP when it is writing an index on IP,
@@ -1688,13 +1687,8 @@ int ltfs_mount(bool force_full, bool deep_recovery, bool recover_extra, bool rec
16881687
(unsigned long long)vol->dp_coh.volume_change_ref,
16891688
(unsigned long long)volume_change_ref);
16901689

1691-
if (vollock == PWE_MAM_BOTH) {
1692-
/* Index of IP could be corrupted (because of double write perm). So set skip flag to true */
1693-
ret = _ltfs_search_index_wp(recover_symlink, true, &seekpos, vol);
1694-
} else {
1695-
/* Index of DP could be corrupted. So set skip flag to false */
1696-
ret = _ltfs_search_index_wp(recover_symlink, false, &seekpos, vol);
1697-
}
1690+
/* Index of IP could be corrupted. So set skip flag */
1691+
ret = _ltfs_search_index_wp(recover_symlink, true, &seekpos, vol);
16981692
if (ret < 0)
16991693
goto out_unlock;
17001694

src/libltfs/ltfs_fsops.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,12 @@ int ltfs_fsops_symlink_path(const char* to, const char* from, ltfs_file_id *id,
19121912

19131913
id->uid = d->uid;
19141914
id->ino = d->ino;
1915+
// TODO: Check if the return error is needed or can be skipped.
19151916
d->target.name = strdup(to);
1917+
if (! d->target.name) {
1918+
ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_symlink_path: d target name");
1919+
return -LTFS_NO_MEMORY;
1920+
}
19161921
d->target.percent_encode = fs_is_percent_encode_required(to);
19171922
d->isslink = true;
19181923

src/libltfs/ltfs_internal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,10 @@ int ltfs_split_symlink( struct ltfs_volume *vol )
13131313
}
13141314
ret = ltfs_fsops_close( workd, true, true, use_iosche, vol);
13151315
path=strdup(lfdir);
1316+
if (! path) {
1317+
ltfsmsg(LTFS_ERR, 10001E, "ltfs_split_symlink: path assign");
1318+
return -LTFS_NO_MEMORY;
1319+
}
13161320

13171321
/* loop for conflicted files */
13181322
for( i=0; i<(vol->index->symerr_count); i++ ){

src/libltfs/ltfslogging.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id,
489489
vsprintf(msg_buf, output_buf, argp);
490490
va_end(argp);
491491
*msg_out = strdup(msg_buf);
492+
// TODO: Check if the return -1 is needed, or the program can just continue
493+
if (!(*msg_out)) {
494+
ltfsmsg(LTFS_ERR, 10001E, "ltfsmsg_internal: msg_out assign");
495+
return -1;
496+
}
492497
}
493498

494499
#ifdef ENABLE_SNMP

src/libltfs/ltfssnmp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ int read_trap_def_file(char *deffile)
122122
return -LTFS_NO_MEMORY;
123123
}
124124
entry->id = strdup(tok);
125+
if (!entry->id) {
126+
ltfsmsg(LTFS_ERR, 10001E, "read_trap_def_file: entry id assign");
127+
return -LTFS_NO_MEMORY;
128+
}
125129
TAILQ_INSERT_TAIL(&trap_entries, entry, list);
126130
}
127131
}

src/libltfs/pathname.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,10 @@ int _pathname_utf8_to_system_icu(const char *src, char **dest)
953953
syslocale = ucnv_getDefaultName();
954954
if (! strcmp(syslocale, "UTF-8")) {
955955
*dest = strdup(src);
956-
if (! *dest)
956+
if (! (*dest)) {
957+
ltfsmsg(LTFS_ERR, 10001E, "_pathname_utf8_to_system_icu: dest assign");
957958
return -LTFS_NO_MEMORY;
959+
}
958960
return 0;
959961
}
960962

src/libltfs/tape.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,10 @@ int tape_get_media_pool_info(struct ltfs_volume *vol, char **media_name, char **
18951895
name = strndup(vol->t_attr->media_pool, add_start);
18961896
}
18971897
info = strdup(&(vol->t_attr->media_pool[add_start+1]));
1898+
if (!info) {
1899+
ltfsmsg(LTFS_ERR, 10001E, __FILE__);
1900+
return -LTFS_NO_MEMORY;
1901+
}
18981902
len = strlen(info);
18991903
info[len-1] = '\0';
19001904
}
@@ -3508,7 +3512,7 @@ int read_tape_attribute(struct ltfs_volume *vol, char **val, const char *name)
35083512
*val = strdup(vol->t_attr->media_pool);
35093513
}
35103514

3511-
if (!*val) {
3515+
if (!(*val)) {
35123516
ltfsmsg(LTFS_ERR, 10001E, "read_tape_attribute: *val");
35133517
return -LTFS_UNEXPECTED_VALUE;
35143518
}

0 commit comments

Comments
 (0)