[Ocfs2-tools-devel] [PATCH 1/6] debugfs.ocfs2: Check the validity of variables before use

piaojun piaojun at huawei.com
Wed Mar 25 23:32:38 PDT 2015


1. In do_command(), we need check if args is null before use.

2. In do_close(), we need check if args and args[0] are null before use.

3. In do_rdump() and inode_time_to_str(), we need check if tm is null
   before use.

4. In dump_inode(), we need check if flags and dyn_features are null before
   use.

5. In dump_dir_entry(), we need check if ocfs2_read_inode() executes
   successfully.

6. In dump_dx_root(), we need check if flags is null before use.

7. In dump_jbd_header(), we need check if jstr is null before use.

8. In dump_jbd_block(), we need check if tagflg is null before use.

9. In dump_lock(), we need check if action is null before use.

10. In dump_lockres(), we need check if state and lists are null before
    use.

11. In process_one_list(), we need check if entry is null before use.

12. In set_logmode_sysfs(), we need check if logpath is null before use.

13. In main(), we need check if line is null before use.

Signed-off-by: Jun Piao <piaojun at huawei.com>
Reviewed-by: Alex Chen <alex.chen at huawei.com>

---
 debugfs.ocfs2/commands.c       |  7 +++++--
 debugfs.ocfs2/dump.c           | 19 +++++++++++++++----
 debugfs.ocfs2/dump_dlm_locks.c | 16 +++++++++++-----
 debugfs.ocfs2/main.c           | 12 ++++++++++++
 debugfs.ocfs2/utils.c          |  3 ++-
 5 files changed, 45 insertions(+), 12 deletions(-)

diff --git a/debugfs.ocfs2/commands.c b/debugfs.ocfs2/commands.c
index 1b0b2d9..d0a85cd 100644
--- a/debugfs.ocfs2/commands.c
+++ b/debugfs.ocfs2/commands.c
@@ -329,7 +329,8 @@ void do_command(char *cmd)
 		return;

 	args = g_strsplit(cmd, " ", -1);
-
+	if (!args)
+		return;
 	/* Move empty strings to the end */
 	crunch_strsplit(args);

@@ -755,7 +756,7 @@ static void do_close(char **args)
 		return ;

 	ret = ocfs2_close(gbls.fs);
-	if (ret)
+	if (ret && args && args[0])
 		com_err(args[0], ret, "while closing context");
 	gbls.fs = NULL;
 	gbls.imagefile = 0;
@@ -1610,6 +1611,8 @@ static void do_rdump(char **args)

 		time(&tt);
 		tm = localtime(&tt);
+		if (!tm)
+			return;
 		/* YYYY-MM-DD_HH:MI:SS */
 		snprintf(tmp_str, sizeof(tmp_str), "%4d-%2d-%2d_%02d:%02d:%02d",
 			 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday,
diff --git a/debugfs.ocfs2/dump.c b/debugfs.ocfs2/dump.c
index da95a14..902cca0 100644
--- a/debugfs.ocfs2/dump.c
+++ b/debugfs.ocfs2/dump.c
@@ -227,6 +227,8 @@ void dump_inode(FILE *out, struct ocfs2_dinode *in)
 	mode = in->i_mode & (S_IRWXU | S_IRWXG | S_IRWXO);

 	flags = g_string_new(NULL);
+	if (!flags)
+		goto bail;
 	if (in->i_flags & OCFS2_VALID_FL)
 		g_string_append(flags, "Valid ");
 	if (in->i_flags & OCFS2_UNUSED2_FL)
@@ -253,6 +255,8 @@ void dump_inode(FILE *out, struct ocfs2_dinode *in)
 		g_string_append(flags, "Dealloc ");

 	dyn_features = g_string_new(NULL);
+	if (!dyn_features)
+		goto bail;
 	if (in->i_dyn_features & OCFS2_INLINE_DATA_FL)
 		g_string_append(dyn_features, "InlineData ");
 	if (in->i_dyn_features & OCFS2_HAS_XATTR_FL)
@@ -347,7 +351,7 @@ void dump_inode(FILE *out, struct ocfs2_dinode *in)
 		fprintf(out, "\tIndexed Tree Root: %"PRIu64"\n",
 			(uint64_t)in->i_dx_root);
 	}
-
+bail:
 	if (flags)
 		g_string_free(flags, 1);
 	if (dyn_features)
@@ -511,7 +515,7 @@ int dump_dir_entry(struct ocfs2_dir_entry *rec, uint64_t blocknr, int offset,
 	struct ocfs2_dinode *di;
 	char perms[20];
 	char timestr[40];
-
+	errcode_t ret;
 	rec->name[rec->name_len] = '\0';

 	if (!ls->long_opt) {
@@ -520,7 +524,9 @@ int dump_dir_entry(struct ocfs2_dir_entry *rec, uint64_t blocknr, int offset,
 			rec->rec_len, rec->name_len, rec->file_type, rec->name);
 	} else {
 		memset(ls->buf, 0, ls->fs->fs_blocksize);
-		ocfs2_read_inode(ls->fs, rec->inode, ls->buf);
+		ret = ocfs2_read_inode(ls->fs, rec->inode, ls->buf);
+		if (ret)
+			return ret;
 		di = (struct ocfs2_dinode *)ls->buf;

 		inode_perms_to_str(di->i_mode, perms, sizeof(perms));
@@ -623,6 +629,8 @@ void dump_dx_root(FILE *out, struct ocfs2_dx_root_block *dr)
 	GString *flags = NULL;

 	flags = g_string_new(NULL);
+	if (!flags)
+		return;
 	if (dr->dr_flags & OCFS2_DX_FLAG_INLINE)
 		g_string_append(flags, "Inline ");

@@ -744,6 +752,8 @@ void dump_jbd_header(FILE *out, journal_header_t *header)
 	GString *jstr = NULL;

 	jstr = g_string_new(NULL);
+	if (!jstr)
+		return;
 	get_journal_block_type(ntohl(header->h_blocktype), jstr);

 	fprintf(out, "\tSeq: %u   Type: %d (%s)\n", ntohl(header->h_sequence),
@@ -824,7 +834,8 @@ void dump_jbd_block(FILE *out, journal_superblock_t *jsb,
 	int tag_bytes = ocfs2_journal_tag_bytes(jsb);

 	tagflg = g_string_new(NULL);
-
+	if (!tagflg)
+		return;
 	fprintf(out, "\tBlock %"PRIu64": ", blknum);

 	switch (ntohl(header->h_blocktype)) {
diff --git a/debugfs.ocfs2/dump_dlm_locks.c b/debugfs.ocfs2/dump_dlm_locks.c
index 6344ff1..8fa4d11 100644
--- a/debugfs.ocfs2/dump_dlm_locks.c
+++ b/debugfs.ocfs2/dump_dlm_locks.c
@@ -68,7 +68,8 @@ static void dump_lock(struct lock *lock, char *queue, FILE *out)
 	char *ast, *bast, level[5], conv[5];

 	action = g_string_new(NULL);
-
+	if (!action)
+		return;
 	get_lock_level(lock->type, level, sizeof(level));
 	get_lock_level(lock->convert_type, conv, sizeof(conv));

@@ -131,7 +132,10 @@ static void dump_lockres(char *name, struct lockres *res, FILE *out)

 	state = g_string_new(NULL);
 	lists = g_string_new(NULL);
-
+	if (!state)
+		goto out;
+	if (!lists)
+		goto out;
 	if (res->purge)
 		g_string_append(lists, "Purge ");
 	if (res->dirty)
@@ -208,9 +212,11 @@ static void dump_lockres(char *name, struct lockres *res, FILE *out)
 	}

 	fprintf(out, "\n");
-
-	g_string_free(state, 1);
-	g_string_free(lists, 1);
+out:
+	if (state)
+		(void)g_string_free(state, 1);
+	if (lists)
+		(void)g_string_free(lists, 1);
 }

 static int read_lvbx(char *line, struct lockres *res)
diff --git a/debugfs.ocfs2/main.c b/debugfs.ocfs2/main.c
index 5d1e014..2fc02b8 100644
--- a/debugfs.ocfs2/main.c
+++ b/debugfs.ocfs2/main.c
@@ -85,6 +85,8 @@ static void process_one_list(GList *list, char *action)
 			entry->action = action;
 		} else {
 			entry = g_new(struct log_entry, 1);
+			if (!entry)
+				return;
 			entry->action = action;
 			entry->mask = list->data;
 			loglist = g_list_append(loglist, entry);
@@ -346,6 +348,11 @@ static int set_logmode_sysfs(const char *path, struct log_entry *entry)
 	char *logpath;

 	logpath = g_strdup_printf(LOG_CTL_SYSFS_FORMAT, path, entry->mask);
+	if (!logpath) {
+		fprintf(stderr, "%s: memory allocated failed for logpath.\n",
+				gbls.progname);
+		return 1;
+	}
 	f = fopen(logpath, "w");
 	g_free(logpath);
 	if (!f) {
@@ -489,6 +496,11 @@ int main(int argc, char **argv)
 			line = g_strdup_printf("open %s -s %u", opts.device, opts.sb_num);
 		else
 			line = g_strdup_printf("open %s", opts.device);
+		if (!line) {
+			fprintf(stderr, "%s: memory allocated failed for line.\n",
+					argv[0]);
+			goto bail;
+		}
 		do_command(line);
 		g_free(line);
 	}
diff --git a/debugfs.ocfs2/utils.c b/debugfs.ocfs2/utils.c
index 4e7f1b0..ea2e7d8 100644
--- a/debugfs.ocfs2/utils.c
+++ b/debugfs.ocfs2/utils.c
@@ -571,7 +571,8 @@ void inode_time_to_str(uint64_t timeval, char *str, int len)
 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

 	tm = localtime(&tt);
-
+	if (!tm)
+		return;
 	snprintf(str, len, "%2d-%s-%4d %02d:%02d", tm->tm_mday,
 		 month_str[tm->tm_mon], 1900 + tm->tm_year,
 		 tm->tm_hour, tm->tm_min);
-- 
1.8.4.3




More information about the Ocfs2-tools-devel mailing list