From patchwork Wed Apr 13 15:18:48 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artem Bityutskiy X-Patchwork-Id: 91038 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 5F4F0B6F07 for ; Thu, 14 Apr 2011 01:19:27 +1000 (EST) Received: from canuck.infradead.org ([2001:4978:20e::1]) by bombadil.infradead.org with esmtps (Exim 4.72 #1 (Red Hat Linux)) id 1QA1og-0005t5-Iq; Wed, 13 Apr 2011 15:17:14 +0000 Received: from localhost ([127.0.0.1] helo=canuck.infradead.org) by canuck.infradead.org with esmtp (Exim 4.72 #1 (Red Hat Linux)) id 1QA1of-0003Zu-3d; Wed, 13 Apr 2011 15:17:13 +0000 Received: from smtp.nokia.com ([147.243.1.48] helo=mgw-sa02.nokia.com) by canuck.infradead.org with esmtps (Exim 4.72 #1 (Red Hat Linux)) id 1QA1nO-0003QG-02 for linux-mtd@lists.infradead.org; Wed, 13 Apr 2011 15:16:01 +0000 Received: from nokia.com (localhost [127.0.0.1]) by mgw-sa02.nokia.com (Switch-3.4.3/Switch-3.4.3) with ESMTP id p3DFFnow018691 for ; Wed, 13 Apr 2011 18:15:52 +0300 Received: from eru.research.nokia.com ([[172.21.23.107]]) by mgw-sa02.nokia.com with ESMTP id p3DFFZvs018457 ; Wed, 13 Apr 2011 18:15:38 +0300 From: Artem Bityutskiy To: MTD list Subject: [PATCH 08/27] fs-tests: integck: clean-up copy_string Date: Wed, 13 Apr 2011 18:18:48 +0300 Message-Id: <1302707947-6143-9-git-send-email-dedekind1@gmail.com> X-Mailer: git-send-email 1.7.2.3 In-Reply-To: <1302707947-6143-1-git-send-email-dedekind1@gmail.com> References: <1302707947-6143-1-git-send-email-dedekind1@gmail.com> X-Nokia-AV: Clean X-CRM114-Version: 20090807-BlameThorstenAndJenny ( TRE 0.7.6 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20110413_111554_586899_5A335B5D X-CRM114-Status: GOOD ( 11.73 ) X-Spam-Score: 3.4 (+++) X-Spam-Report: SpamAssassin version 3.3.1 on canuck.infradead.org summary: Content analysis details: (3.4 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, low trust [147.243.1.48 listed in list.dnswl.org] 0.0 FREEMAIL_FROM Sender email is freemail (dedekind1[at]gmail.com) 0.0 DKIM_ADSP_CUSTOM_MED No valid author signature, adsp_override is CUSTOM_MED 2.2 FREEMAIL_ENVFROM_END_DIGIT Envelope-from freemail username ends in digit (dedekind1[at]gmail.com) 1.2 NML_ADSP_CUSTOM_MED ADSP custom_med hit, and not from a mailing list Cc: Adrian Hunter X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Artem Bityutskiy 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 --- 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 #include #include +#include #include #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);