Guard in public API agains bad names or link destinations.

This commit is contained in:
Vreixo Formoso 2007-12-24 04:20:29 +01:00
parent 227b17f251
commit 9de0be2602
2 changed files with 28 additions and 8 deletions

View File

@ -316,8 +316,13 @@ enum IsoNodeType iso_node_get_type(IsoNode *node);
* Set the name of a node. Note that if the node is already added to a dir * Set the name of a node. Note that if the node is already added to a dir
* this can fail if dir already contains a node with the new name. * this can fail if dir already contains a node with the new name.
* *
* @param node
* The node whose name you want to change. Note that you can't change
* the name of the root.
* @param name * @param name
* The name in UTF-8 encoding * The name in UTF-8 encoding. If you supply an empty string or a
* name greater than 255 characters this returns with failure, and
* node name is not modified.
* @return * @return
* 1 on success, < 0 on error * 1 on success, < 0 on error
*/ */
@ -614,6 +619,10 @@ const char *iso_symlink_get_dest(const IsoSymlink *link);
/** /**
* Set the destination of a link. * Set the destination of a link.
*
* @param dest
* New destination for the link. It must be a non-empty string, otherwise
* this function doesn't modify previous destination.
*/ */
void iso_symlink_set_dest(IsoSymlink *link, const char *dest); void iso_symlink_set_dest(IsoSymlink *link, const char *dest);

View File

@ -79,20 +79,27 @@ enum IsoNodeType iso_node_get_type(IsoNode *node)
int iso_node_set_name(IsoNode *node, const char *name) int iso_node_set_name(IsoNode *node, const char *name)
{ {
char *new; char *new;
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;
}
}
/* guard against the empty string */ /* guard against the empty string */
if (name[0] == '\0') { if (name[0] == '\0' || strlen(name) > 255) {
return ISO_WRONG_ARG_VALUE; 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); new = strdup(name);
if (new == NULL) { if (new == NULL) {
return ISO_MEM_ERROR; 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); free(node->name);
node->name = new; node->name = new;
if (node->parent != NULL) { if (node->parent != NULL) {
@ -537,6 +544,10 @@ const char *iso_symlink_get_dest(const IsoSymlink *link)
*/ */
void iso_symlink_set_dest(IsoSymlink *link, const char *dest) void iso_symlink_set_dest(IsoSymlink *link, const char *dest)
{ {
if (dest == NULL || dest[0] == '\0') {
/* guard against null or empty dest */
return;
}
free(link->dest); free(link->dest);
link->dest = strdup(dest); link->dest = strdup(dest);
} }