All of lore.kernel.org
 help / color / mirror / Atom feed
From: Artem Bityutskiy <dedekind1@gmail.com>
To: MTD list <linux-mtd@lists.infradead.org>
Cc: Adrian Hunter <adrian.hunter@nokia.com>
Subject: [PATCH 08/27] fs-tests: integck: clean-up copy_string
Date: Wed, 13 Apr 2011 18:18:48 +0300	[thread overview]
Message-ID: <1302707947-6143-9-git-send-email-dedekind1@gmail.com> (raw)
In-Reply-To: <1302707947-6143-1-git-send-email-dedekind1@gmail.com>

From: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>

This is a clean-up patch which:
1. Simplifies copy_string by useng strdup instead of malloc
2. Adds an assertion which checks the input parameter agains NULL,
   instead of checking it.
3. Re-names it to dup_string which looks more readable to me.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
---
 tests/fs-tests/integrity/integck.c |   46 ++++++++++++++++++-----------------
 1 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/tests/fs-tests/integrity/integck.c b/tests/fs-tests/integrity/integck.c
index 281de81..a225042 100644
--- a/tests/fs-tests/integrity/integck.c
+++ b/tests/fs-tests/integrity/integck.c
@@ -30,6 +30,7 @@
 #include <limits.h>
 #include <dirent.h>
 #include <getopt.h>
+#include <assert.h>
 #include <sys/mman.h>
 #include "tests.h"
 
@@ -172,15 +173,16 @@ static void *zalloc(size_t size)
 	return buf;
 }
 
-static char *copy_string(const char *s)
+/*
+ * Duplicate a string.
+ */
+static char *dup_string(const char *s)
 {
 	char *str;
 
-	if (!s)
-		return NULL;
-	str = malloc(strlen(s) + 1);
+	assert(s != NULL);
+	str = strdup(s);
 	CHECK(str != NULL);
-	strcpy(str, s);
 	return str;
 }
 
@@ -190,9 +192,9 @@ static char *cat_strings(const char *a, const char *b)
 	size_t sz;
 
 	if (a && !b)
-		return copy_string(a);
+		return dup_string(a);
 	if (b && !a)
-		return copy_string(b);
+		return dup_string(b);
 	if (!a && !b)
 		return NULL;
 	sz = strlen(a) + strlen(b) + 1;
@@ -211,9 +213,9 @@ static char *cat_paths(const char *a, const char *b)
 	size_t na, nb;
 
 	if (a && !b)
-		return copy_string(a);
+		return dup_string(a);
 	if (b && !a)
-		return copy_string(b);
+		return dup_string(b);
 	if (!a && !b)
 		return NULL;
 
@@ -301,7 +303,7 @@ static void add_dir_entry(struct dir_info *parent, char type, const char *name,
 
 	entry = zalloc(sizeof(struct dir_entry_info));
 	entry->type = type;
-	entry->name = copy_string(name);
+	entry->name = dup_string(name);
 	entry->parent = parent;
 
 	entry->next = parent->first;
@@ -324,7 +326,7 @@ static void add_dir_entry(struct dir_info *parent, char type, const char *name,
 
 		entry->dir = dir;
 		dir->entry = entry;
-		dir->name = copy_string(name);
+		dir->name = dup_string(name);
 		dir->parent = parent;
 	} else if (entry->type == 's') {
 		struct symlink_info *symlink = target;
@@ -377,7 +379,7 @@ static struct dir_info *dir_new(struct dir_info *parent, const char *name)
 	free(path);
 
 	dir = zalloc(sizeof(struct dir_info));
-	dir->name = copy_string(name);
+	dir->name = dup_string(name);
 	dir->parent = parent;
 	if (parent)
 		add_dir_entry(parent, 'd', name, dir);
@@ -435,7 +437,7 @@ static struct file_info *file_new(struct dir_info *parent, const char *name)
 	free(path);
 
 	file = zalloc(sizeof(struct file_info));
-	file->name = copy_string(name);
+	file->name = dup_string(name);
 
 	add_dir_entry(parent, 'f', name, file);
 
@@ -1210,7 +1212,7 @@ static char *symlink_path(const char *path, const char *target_pathname)
 	size_t len, totlen, tarlen;
 
 	if (target_pathname[0] == '/')
-		return copy_string(target_pathname);
+		return dup_string(target_pathname);
 	p = strrchr(path, '/');
 	len = p - path;
 	len += 1;
@@ -1479,7 +1481,7 @@ static char *pick_rename_name(struct dir_info **parent,
 	*rename_entry = NULL;
 
 	if (grow || tests_random_no(20) < 10)
-		return copy_string(make_name(dir));
+		return dup_string(make_name(dir));
 
 	r = tests_random_no(dir->number_of_entries);
 	entry = dir->first;
@@ -1491,14 +1493,14 @@ static char *pick_rename_name(struct dir_info **parent,
 		entry = dir->first;
 	if (!entry ||
 	    (entry->type == 'd' && entry->dir->number_of_entries != 0))
-		return copy_string(make_name(dir));
+		return dup_string(make_name(dir));
 
 	if ((isdir && entry->type != 'd') ||
 	    (!isdir && entry->type == 'd'))
-		return copy_string(make_name(dir));
+		return dup_string(make_name(dir));
 
 	*rename_entry = entry;
-	return copy_string(entry->name);
+	return dup_string(entry->name);
 }
 
 static void rename_entry(struct dir_entry_info *entry)
@@ -1593,13 +1595,13 @@ static char *relative_path(const char *path1, const char *path2)
 	len2 = strlen(p2);
 	up = str_count(p1, '/');
 	if (up == 0 && len2 != 0)
-		return copy_string(p2);
+		return dup_string(p2);
 	if (up == 0 && len2 == 0) {
 		p2 = strrchr(path2, '/');
-		return copy_string(p2);
+		return dup_string(p2);
 	}
 	if (up == 1 && len2 == 0)
-		return copy_string(".");
+		return dup_string(".");
 	if (len2 == 0)
 		up -= 1;
 	len = up * 3 + len2 + 1;
@@ -1651,7 +1653,7 @@ static char *pick_symlink_target(const char *symlink_path)
 static void symlink_new(struct dir_info *dir, const char *name_)
 {
 	struct symlink_info *s;
-	char *path, *target, *name = copy_string(name_);
+	char *path, *target, *name = dup_string(name_);
 
 	path = dir_path(dir, name);
 	target = pick_symlink_target(path);
-- 
1.7.2.3

  parent reply	other threads:[~2011-04-13 15:16 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-13 15:18 [PATCH 00/27] mtd-utils: make integck test independent Artem Bityutskiy
2011-04-13 15:16 ` Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 01/27] fs-tests: integck: shrink dir_entry_info structure size Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 02/27] fs-tests: integck: shrink file_info " Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 03/27] fs-tests: integck: srink file_info structure even more Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 04/27] fs-tests: integck: abuse random_offset field nicer Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 05/27] fs-tests: integck: shrink write_info even more Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 06/27] fs-tests: integck: change tests defaults Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 07/27] fs-tests: integck: implement own parameters parsing Artem Bityutskiy
2011-04-13 15:18 ` Artem Bityutskiy [this message]
2011-04-13 15:18 ` [PATCH 09/27] fs-tests: integck: get rid of tests_check_test_file_system Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 10/27] fs-tests: integck: make integck function return error Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 11/27] fs-tests: integck: move mem_page_size to fsinfo Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 12/27] fs-tests: integck: move more variables " Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 13/27] fs-tests: integck: add own get_free_space function Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 14/27] fs-tests: integck: move log10_initial_free to fsinfo Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 15/27] fs-tests: integck: remove trailing backslashes from mount point Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 16/27] fs-tests: integck: do not use tests_cat_pid Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 17/27] fs-tests: integck: clean up test directory creation Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 18/27] fs-tests: integck: do not use tests_clear_dir Artem Bityutskiy
2011-04-13 15:18 ` [PATCH 19/27] fs-tests: integck: do not use space after cast Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 20/27] fs-tests: integck: prefer to check for success Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 21/27] fs-tests: integck: do not use tests_fs_is_rootfs Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 22/27] fs-tests: integck: do not use tests_remount Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 23/27] fs-tests: integck: do not use tests_get_total_space Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 24/27] fs-tests: integck: do not use global common variables Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 25/27] fs-tests: integck: do not use tests_random_no Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 26/27] fs-tests: integck: implement own version of CHECK Artem Bityutskiy
2011-04-13 15:19 ` [PATCH 27/27] fs-tests: integck: do not expect max name length to be 256 Artem Bityutskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1302707947-6143-9-git-send-email-dedekind1@gmail.com \
    --to=dedekind1@gmail.com \
    --cc=adrian.hunter@nokia.com \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.