Browse Source

Ensure names of public tree IsoNodes are valid POSIX names.

release-1.5.4.branch
Vreixo Formoso 15 years ago
parent
commit
1459801134
  1. 60
      src/node.c
  2. 8
      src/node.h
  3. 15
      src/tree.c

60
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;
}
new = strdup(name);
if (new == NULL) {
return ISO_MEM_ERROR;
/* 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) {
free(new);
return ISO_NODE_NAME_NOT_UNIQUE;
}
}
new = strdup(name);
if (new == NULL) {
return ISO_MEM_ERROR;
}
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;

8
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_*/

15
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);

Loading…
Cancel
Save