X-Git-Url: http://www.privoxy.org/gitweb/misc.html?a=blobdiff_plain;f=list.c;h=a2d7ff6bc5758d30a4c4b9f02096fff10af73817;hb=fa9d00715c824a1db96c3e6dea5418f159326873;hp=224c52f58479b59994c493306b938e23ff661906;hpb=1b2cc4af84a3bc3ba01ba913ca30012c9efa31d8;p=privoxy.git
diff --git a/list.c b/list.c
index 224c52f5..a2d7ff6b 100644
--- a/list.c
+++ b/list.c
@@ -1,14 +1,11 @@
-const char list_rcs[] = "$Id: list.c,v 1.23 2011/01/22 12:30:22 fabiankeil Exp $";
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/list.c,v $
*
* Purpose : Declares functions to handle lists.
- * Functions declared include:
- * `destroy_list', `enlist' and `list_to_text'
*
- * Copyright : Written by and Copyright (C) 2001-2007 the SourceForge
- * Privoxy team. http://www.privoxy.org/
+ * Copyright : Written by and Copyright (C) 2001-2007 members of the
+ * Privoxy team. https://www.privoxy.org/
*
* Based on the Internet Junkbuster originally written
* by and Copyright (C) 1997 Anonymous Coders and
@@ -48,7 +45,7 @@ const char list_rcs[] = "$Id: list.c,v 1.23 2011/01/22 12:30:22 fabiankeil Exp $
#endif
#include
-#if !defined(_WIN32) && !defined(__OS2__)
+#if !defined(_WIN32)
#include
#endif
@@ -58,11 +55,9 @@ const char list_rcs[] = "$Id: list.c,v 1.23 2011/01/22 12:30:22 fabiankeil Exp $
#include "list.h"
#include "miscutil.h"
-const char list_h_rcs[] = LIST_H_VERSION;
-
-
+#ifndef NDEBUG
static int list_is_valid (const struct list *the_list);
-
+#endif
/*********************************************************************
*
@@ -129,6 +124,7 @@ void destroy_list (struct list *the_list)
}
+#ifndef NDEBUG
/*********************************************************************
*
* Function : list_is_valid
@@ -136,7 +132,7 @@ void destroy_list (struct list *the_list)
* Description : Check that a string list is valid. The intended
* usage is "assert(list_is_valid(the_list))".
* Currently this checks that "the_list->last"
- * is correct, and that the list dosn't contain
+ * is correct, and that the list doesn't contain
* circular references. It is likely to crash if
* it's passed complete garbage.
*
@@ -148,11 +144,6 @@ void destroy_list (struct list *the_list)
*********************************************************************/
static int list_is_valid (const struct list *the_list)
{
- /*
- * If you don't want this check, just change the line below
- * from "#if 1" to "#if 0".
- */
-#if 1
const struct list_entry *cur_entry;
const struct list_entry *last_entry = NULL;
int entry = 0;
@@ -185,13 +176,13 @@ static int list_is_valid (const struct list *the_list)
* Note that the 1000 limit was hit by a real user in tracker 911950;
* removing it for now. Real circular references should eventually
* be caught by the check above, anyway.
- */
+ */
/*
if (entry > 1000)
- {
+ {
return 0;
- }
- */
+ }
+ */
/*
* Check this isn't marked as the last entry, unless of course it's
@@ -205,10 +196,10 @@ static int list_is_valid (const struct list *the_list)
}
return (the_list->last == last_entry);
-#else
- return 1;
-#endif
+
}
+#endif /* ndef NDEBUG */
+
/*********************************************************************
*
@@ -353,7 +344,7 @@ jb_err enlist_unique(struct list *the_list, const char *str,
{
for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
{
- if ( (cur_entry->str != NULL)
+ if ((cur_entry->str != NULL)
&& (0 == strncmp(str, cur_entry->str, num_significant_chars)))
{
/* Already there */
@@ -366,7 +357,7 @@ jb_err enlist_unique(struct list *the_list, const char *str,
/* Test whole string */
for (cur_entry = the_list->first; cur_entry != NULL; cur_entry = cur_entry->next)
{
- if ( (cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
+ if ((cur_entry->str != NULL) && (0 == strcmp(str, cur_entry->str)))
{
/* Already there */
return JB_ERR_OK;
@@ -417,8 +408,24 @@ jb_err enlist_unique_header(struct list *the_list, const char *name,
if (NULL != header)
{
const size_t bytes_to_compare = strlen(name) + 2;
+ char *p = header;
snprintf(header, header_size, "%s: %s", name, value);
+ /*
+ * The trailing "\r\n" is added by list_to_text(),
+ * if the caller passed them anyway, cut the header
+ * at the first one or dump core if this is a debug
+ * build.
+ */
+ do
+ {
+ if ((*p == '\r') || (*p == '\n'))
+ {
+ assert(*p != '\r');
+ assert(*p != '\n');
+ *p = '\0';
+ }
+ } while (*p++);
result = enlist_unique(the_list, header, bytes_to_compare);
free(header);
assert(list_is_valid(the_list));
@@ -839,7 +846,7 @@ int list_contains_item(const struct list *the_list, const char *str)
if (entry->str == NULL)
{
/*
- * NULL pointers are allowed in some lists.
+ * NULL pointers are allowed in some lists.
* For example for csp->headers in case a
* header was removed.
*/
@@ -862,15 +869,24 @@ int list_contains_item(const struct list *the_list, const char *str)
* Function : new_map
*
* Description : Create a new, empty map.
+ * Causes program exit if the memory allocation fails.
*
* Parameters : N/A
*
- * Returns : A new, empty map, or NULL if out of memory.
+ * Returns : A new, empty map
*
*********************************************************************/
struct map *new_map(void)
{
- return (struct map *) zalloc(sizeof(struct map));
+ struct map *empty_map = zalloc(sizeof(struct map));
+
+ if (NULL == empty_map)
+ {
+ exit(1);
+ }
+
+ return empty_map;
+
}
@@ -879,7 +895,7 @@ struct map *new_map(void)
* Function : free_map
*
* Description : Free the memory occupied by a map and its
- * depandant strings
+ * dependent strings
*
* Parameters :
* 1 : the_map = map to be freed. May be NULL.
@@ -965,7 +981,7 @@ jb_err map(struct map *the_map,
if ( (NULL == value)
|| (NULL == name)
- || (NULL == (new_entry = zalloc(sizeof(*new_entry)))) )
+ || (NULL == (new_entry = zalloc(sizeof(*new_entry)))))
{
if ((name != NULL) && (!name_needs_copying))
{
@@ -1040,8 +1056,8 @@ jb_err unmap(struct map *the_map, const char *name)
assert(the_map);
assert(name);
-
- last_entry = the_map->first;
+
+ last_entry = NULL;
for (cur_entry = the_map->first; cur_entry != NULL; cur_entry = cur_entry->next)
{
@@ -1060,20 +1076,24 @@ jb_err unmap(struct map *the_map, const char *name)
}
/*
- * Update the map's last pointer
+ * Update the map's last pointer
*/
if (cur_entry == the_map->last)
{
the_map->last = last_entry;
}
-
+
/*
* Free the map_entry
*/
freez(cur_entry->name);
freez(cur_entry->value);
freez(cur_entry);
-
+ if (last_entry == NULL)
+ {
+ /* The map only had a single entry which has just been removed. */
+ break;
+ }
cur_entry = last_entry;
}
else
@@ -1097,7 +1117,7 @@ jb_err unmap(struct map *the_map, const char *name)
* 2 : name = name parameter to look for
*
* Returns : the value if found, else the empty string.
- * Return value is alloced as part of the map, so
+ * Return value is allocated as part of the map, so
* it is freed when the map is destroyed. Caller
* must not free or modify it.
*