Commit e56f4d8a authored by conrad's avatar conrad
Browse files

Mozilla #520500: Allow NULL-value comments

This commit changes the policy of fs_comment_validate_byname() to
allow NULL-value comments, and changes fs_comment_new() to handle
the case where value=NULL.

This commit should pass "make check", including the policy change
introduced in the previous commit.
parent d0b3830b
...@@ -134,11 +134,11 @@ The comment header is decoded as follows: ...@@ -134,11 +134,11 @@ The comment header is decoded as follows:
}while(0) }while(0)
static int static int
fs_comment_validate_byname (const char * name, const char * value) fs_comment_validate_byname (const char * name)
{ {
const char * c; const char * c;
if (!name || !value) return 0; if (!name) return 0;
for (c = name; *c; c++) { for (c = name; *c; c++) {
if (*c < 0x20 || *c > 0x7D || *c == 0x3D) { if (*c < 0x20 || *c > 0x7D || *c == 0x3D) {
...@@ -157,8 +157,8 @@ fs_comment_new (const char * name, const char * value) ...@@ -157,8 +157,8 @@ fs_comment_new (const char * name, const char * value)
{ {
FishSoundComment * comment; FishSoundComment * comment;
if (!fs_comment_validate_byname (name, value)) return NULL; if (!fs_comment_validate_byname (name)) return NULL;
/* Ensures that name != NULL, value != NULL, and validates strings */ /* Ensures that name != NULL and contains only valid characters */
comment = fs_malloc (sizeof (FishSoundComment)); comment = fs_malloc (sizeof (FishSoundComment));
if (comment == NULL) return NULL; if (comment == NULL) return NULL;
...@@ -169,11 +169,15 @@ fs_comment_new (const char * name, const char * value) ...@@ -169,11 +169,15 @@ fs_comment_new (const char * name, const char * value)
return NULL; return NULL;
} }
comment->value = fs_strdup (value); if (value) {
if (comment->value == NULL) { comment->value = fs_strdup (value);
fs_free (comment->name); if (comment->value == NULL) {
fs_free (comment); fs_free (comment->name);
return NULL; fs_free (comment);
return NULL;
}
} else {
comment->value = NULL;
} }
return comment; return comment;
...@@ -241,7 +245,7 @@ fish_sound_comment_first_byname (FishSound * fsound, char * name) ...@@ -241,7 +245,7 @@ fish_sound_comment_first_byname (FishSound * fsound, char * name)
if (name == NULL) return fs_vector_nth (fsound->comments, 0); if (name == NULL) return fs_vector_nth (fsound->comments, 0);
if (!fs_comment_validate_byname (name, "")) if (!fs_comment_validate_byname (name))
return NULL; return NULL;
for (i = 0; i < fs_vector_size (fsound->comments); i++) { for (i = 0; i < fs_vector_size (fsound->comments); i++) {
...@@ -298,7 +302,7 @@ fish_sound_comment_add (FishSound * fsound, FishSoundComment * comment) ...@@ -298,7 +302,7 @@ fish_sound_comment_add (FishSound * fsound, FishSoundComment * comment)
return FISH_SOUND_ERR_INVALID; return FISH_SOUND_ERR_INVALID;
#if FS_ENCODE #if FS_ENCODE
if (!fs_comment_validate_byname (comment->name, comment->value)) if (!fs_comment_validate_byname (comment->name))
return FISH_SOUND_ERR_COMMENT_INVALID; return FISH_SOUND_ERR_COMMENT_INVALID;
if ((new_comment = fs_comment_new (comment->name, comment->value)) == NULL) if ((new_comment = fs_comment_new (comment->name, comment->value)) == NULL)
...@@ -325,7 +329,7 @@ fish_sound_comment_add_byname (FishSound * fsound, const char * name, ...@@ -325,7 +329,7 @@ fish_sound_comment_add_byname (FishSound * fsound, const char * name,
return FISH_SOUND_ERR_INVALID; return FISH_SOUND_ERR_INVALID;
#if FS_ENCODE #if FS_ENCODE
if (!fs_comment_validate_byname (name, value)) if (!fs_comment_validate_byname (name))
return FISH_SOUND_ERR_COMMENT_INVALID; return FISH_SOUND_ERR_COMMENT_INVALID;
if ((comment = fs_comment_new (name, value)) == NULL) if ((comment = fs_comment_new (name, value)) == NULL)
...@@ -484,7 +488,7 @@ fish_sound_comments_decode (FishSound * fsound, unsigned char * comments, ...@@ -484,7 +488,7 @@ fish_sound_comments_decode (FishSound * fsound, unsigned char * comments,
if ((nvalue = fs_strdup_len (value, n)) == NULL) if ((nvalue = fs_strdup_len (value, n)) == NULL)
return FISH_SOUND_ERR_OUT_OF_MEMORY; return FISH_SOUND_ERR_OUT_OF_MEMORY;
debug_printf (1, "%s -> %s (length %d)", name, nvalue, n); debug_printf (1, "[%d] %s -> %s (length %d)", i, name, nvalue, n);
if ((comment = fs_comment_new (name, nvalue)) == NULL) { if ((comment = fs_comment_new (name, nvalue)) == NULL) {
fs_free (nvalue); fs_free (nvalue);
...@@ -498,11 +502,16 @@ fish_sound_comments_decode (FishSound * fsound, unsigned char * comments, ...@@ -498,11 +502,16 @@ fish_sound_comments_decode (FishSound * fsound, unsigned char * comments,
fs_free (nvalue); fs_free (nvalue);
} else { } else {
debug_printf (1, "[%d] %s (no value)", i, name, len); /* For the case of a comment which is not in key=value form,
* duplicate exactly the length of the comment, as it is
* not NUL-terminated. In the case of the last comment of the
* packet, it will be followed immediately by a framing bit.
*/
if ((nvalue = fs_strdup_len (name, len)) == NULL) if ((nvalue = fs_strdup_len (name, len)) == NULL)
return FISH_SOUND_ERR_OUT_OF_MEMORY; return FISH_SOUND_ERR_OUT_OF_MEMORY;
debug_printf (1, "[%d] %s (no value) (length %d)", i, nvalue, len);
if ((comment = fs_comment_new (nvalue, NULL)) == NULL) { if ((comment = fs_comment_new (nvalue, NULL)) == NULL) {
fs_free (nvalue); fs_free (nvalue);
return FISH_SOUND_ERR_OUT_OF_MEMORY; return FISH_SOUND_ERR_OUT_OF_MEMORY;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment