summaryrefslogtreecommitdiff
path: root/src/references.c
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2014-09-10 19:50:29 +0200
committerVicent Marti <tanoku@gmail.com>2014-09-10 19:58:22 +0200
commit8c028e1a88c2d2aac4a4086202568bee43678aa8 (patch)
tree78fc80b00a62a259419b59d54c3b34e8dcdf334b /src/references.c
parent7c2a062cdf9c0514cdf32f4f8bd07cf52d183c8b (diff)
Do not create references with empty names
Diffstat (limited to 'src/references.c')
-rw-r--r--src/references.c31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/references.c b/src/references.c
index 84cb773..300bbcc 100644
--- a/src/references.c
+++ b/src/references.c
@@ -23,15 +23,29 @@ static void reference_free(reference *ref)
// normalize reference: collapse internal whitespace to single space,
// remove leading/trailing whitespace, case fold
+// Return NULL if the reference name is actually empty (i.e. composed
+// solely from whitespace)
static unsigned char *normalize_reference(chunk *ref)
{
strbuf normalized = GH_BUF_INIT;
+ unsigned char *result;
+
+ if (ref->len == 0)
+ return NULL;
utf8proc_case_fold(&normalized, ref->data, ref->len);
strbuf_trim(&normalized);
strbuf_normalize_whitespace(&normalized);
- return strbuf_detach(&normalized);
+ result = strbuf_detach(&normalized);
+ assert(result);
+
+ if (result[0] == '\0') {
+ free(result);
+ return NULL;
+ }
+
+ return result;
}
static void add_reference(reference_map *map, reference* ref)
@@ -51,19 +65,23 @@ static void add_reference(reference_map *map, reference* ref)
map->table[ref->hash % REFMAP_SIZE] = ref;
}
-extern reference *reference_create(reference_map *map, chunk *label, chunk *url, chunk *title)
+extern void reference_create(reference_map *map, chunk *label, chunk *url, chunk *title)
{
reference *ref;
+ unsigned char *reflabel = normalize_reference(label);
+
+ /* empty reference name, or composed from only whitespace */
+ if (reflabel == NULL)
+ return;
+
ref = malloc(sizeof(reference));
- ref->label = normalize_reference(label);
+ ref->label = reflabel;
ref->hash = refhash(ref->label);
ref->url = clean_url(url);
ref->title = clean_title(title);
ref->next = NULL;
add_reference(map, ref);
-
- return ref;
}
// Returns reference if refmap contains a reference with matching
@@ -78,6 +96,9 @@ reference* reference_lookup(reference_map *map, chunk *label)
return NULL;
norm = normalize_reference(label);
+ if (norm == NULL)
+ return NULL;
+
hash = refhash(norm);
ref = map->table[hash % REFMAP_SIZE];