diff --git a/src/node.c b/src/node.c index 326d3cd..52ddc31 100644 --- a/src/node.c +++ b/src/node.c @@ -80,37 +80,27 @@ int iso_node_set_name(IsoNode *node, const char *name) { char *new; - /* guard against the empty string or big names... */ - if (name[0] == '\0' || strlen(name) > 255) { - return ISO_WRONG_ARG_VALUE; - } - - /* ...against "." and ".." names... */ - if (!strcmp(name, ".") || !strcmp(name, "..")) { - return ISO_WRONG_ARG_VALUE; - } - - /* ...and against names with '/' */ - if (strchr(name, '/') != NULL) { - return ISO_WRONG_ARG_VALUE; - } - if ((IsoNode*)node->parent == node) { /* you can't change name of the root node */ return ISO_WRONG_ARG_VALUE; } + + /* check if the name is valid */ + if (!iso_node_is_valid_name(name)) { + return ISO_WRONG_ARG_VALUE; + } + + if (node->parent != NULL) { + /* check if parent already has a node with same name */ + if (iso_dir_get_node(node->parent, name, NULL) == 1) { + return ISO_NODE_NAME_NOT_UNIQUE; + } + } new = strdup(name); if (new == NULL) { return ISO_MEM_ERROR; } - if (node->parent != NULL) { - /* check if parent already has a node with same name */ - if (iso_dir_get_node(node->parent, name, NULL) == 1) { - free(new); - return ISO_NODE_NAME_NOT_UNIQUE; - } - } free(node->name); node->name = new; if (node->parent != NULL) { @@ -595,6 +585,36 @@ int iso_file_get_sort_weight(IsoFile *file) return file->sort_weight; } +/** + * Check if a given name is valid for an iso node. + * + * @return + * 1 if yes, 0 if not + */ +int iso_node_is_valid_name(const char *name) +{ + /* a name can't be NULL */ + if (name == NULL) { + return 0; + } + + /* guard against the empty string or big names... */ + if (name[0] == '\0' || strlen(name) > 255) { + return 0; + } + + /* ...against "." and ".." names... */ + if (!strcmp(name, ".") || !strcmp(name, "..")) { + return 0; + } + + /* ...and against names with '/' */ + if (strchr(name, '/') != NULL) { + return 0; + } + return 1; +} + int iso_node_new_root(IsoDir **root) { IsoDir *dir; diff --git a/src/node.h b/src/node.h index c79980c..f1787f3 100644 --- a/src/node.h +++ b/src/node.h @@ -107,4 +107,12 @@ struct Iso_Dir_Iter int iso_node_new_root(IsoDir **root); +/** + * Check if a given name is valid for an iso node. + * + * @return + * 1 if yes, 0 if not + */ +int iso_node_is_valid_name(const char *name); + #endif /*LIBISO_NODE_H_*/ diff --git a/src/tree.c b/src/tree.c index 0bbfb62..1b92585 100644 --- a/src/tree.c +++ b/src/tree.c @@ -56,6 +56,11 @@ int iso_tree_add_new_dir(IsoDir *parent, const char *name, IsoDir **dir) if (dir) { *dir = NULL; } + + /* check if the name is valid */ + if (!iso_node_is_valid_name(name)) { + return ISO_WRONG_ARG_VALUE; + } /* find place where to insert */ pos = &(parent->children); @@ -140,6 +145,11 @@ int iso_tree_add_new_symlink(IsoDir *parent, const char *name, if (link) { *link = NULL; } + + /* check if the name is valid */ + if (!iso_node_is_valid_name(name)) { + return ISO_WRONG_ARG_VALUE; + } /* find place where to insert */ pos = &(parent->children); @@ -248,6 +258,11 @@ int iso_tree_add_new_special(IsoDir *parent, const char *name, mode_t mode, if (special) { *special = NULL; } + + /* check if the name is valid */ + if (!iso_node_is_valid_name(name)) { + return ISO_WRONG_ARG_VALUE; + } /* find place where to insert */ pos = &(parent->children);