Sun, 13 Jan 2013 13:53:57 -0600
Fix purple_stringref_new()
It was losing the last character of the input string. This was
happening because g_strlcpy() takes the length of the buffer and we
were passing it strlen(value), which would not include the NUL.
This was breaking reading old logs.
| 7763 | 1 | /** |
| 7786 | 2 | * @file stringref.c Reference-counted immutable strings |
| 7763 | 3 | * @ingroup core |
|
20147
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
4 | */ |
|
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
5 | |
|
66f05a854eee
applied changes from 8a731bbd0197fbcc91a705c2d8f528154216defa
Richard Laager <rlaager@pidgin.im>
parents:
19859
diff
changeset
|
6 | /* purple |
| 7763 | 7 | * |
| 15884 | 8 | * Purple is the legal property of its developers, whose names are too numerous |
| 8046 | 9 | * to list here. Please refer to the COPYRIGHT file distributed with this |
| 10 | * source distribution. | |
| 7763 | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify | |
| 13 | * it under the terms of the GNU General Public License as published by | |
| 14 | * the Free Software Foundation; either version 2 of the License, or | |
| 15 | * (at your option) any later version. | |
| 16 | * | |
| 17 | * This program is distributed in the hope that it will be useful, | |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 20 | * GNU General Public License for more details. | |
| 21 | * | |
| 22 | * You should have received a copy of the GNU General Public License | |
| 23 | * along with this program; if not, write to the Free Software | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
15884
diff
changeset
|
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
| 7763 | 25 | * |
| 26 | */ | |
| 27 | ||
| 28 | #include "internal.h" | |
| 29 | ||
| 30 | #include <string.h> | |
| 7767 | 31 | #include <stdarg.h> |
| 7763 | 32 | |
| 7789 | 33 | #include "debug.h" |
|
22351
6ca0640b3d31
Change some g_idle_add(...) calls in libpurple to purple_timeout_add(0, ...)
Mark Doliner <markdoliner@pidgin.im>
parents:
20147
diff
changeset
|
34 | #include "eventloop.h" |
| 7763 | 35 | #include "stringref.h" |
| 36 | ||
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
37 | /** |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
38 | * The internal representation of a stringref. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
39 | * |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
40 | * @note For this structure to be useful, the string contained within |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
41 | * it must be immutable -- for this reason, do _not_ access it |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
42 | * directly! |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
43 | */ |
| 15884 | 44 | struct _PurpleStringref { |
|
14988
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
45 | guint32 ref; /**< The reference count of this string. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
46 | * Note that reference counts are only |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
47 | * 31 bits, and the high-order bit |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
48 | * indicates whether this string is up |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
49 | * for GC at the next idle handler... |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
50 | * But you aren't going to touch this |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
51 | * anyway, right? */ |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
52 | char value[1]; /**< The string contained in this ref. |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
53 | * Notice that it is simply "hanging |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
54 | * off the end" of the ref ... this |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
55 | * is to save an allocation. */ |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
56 | }; |
|
66b34458d49e
[gaim-migrate @ 17698]
Richard Laager <rlaager@pidgin.im>
parents:
14254
diff
changeset
|
57 | |
| 7795 | 58 | #define REFCOUNT(x) ((x) & 0x7fffffff) |
| 59 | ||
| 7786 | 60 | static GList *gclist = NULL; |
| 61 | ||
| 15884 | 62 | static void stringref_free(PurpleStringref *stringref); |
| 7786 | 63 | static gboolean gs_idle_cb(gpointer data); |
| 64 | ||
| 15884 | 65 | PurpleStringref *purple_stringref_new(const char *value) |
| 7763 | 66 | { |
| 15884 | 67 | PurpleStringref *newref; |
|
32024
7d8e1af031fe
Bounds-check stringref copies (which are safe anyway)
Ethan Blanton <elb@pidgin.im>
parents:
22351
diff
changeset
|
68 | size_t len; |
| 7763 | 69 | |
| 7772 | 70 | if (value == NULL) |
| 71 | return NULL; | |
| 72 | ||
|
32024
7d8e1af031fe
Bounds-check stringref copies (which are safe anyway)
Ethan Blanton <elb@pidgin.im>
parents:
22351
diff
changeset
|
73 | len = strlen(value); |
|
7d8e1af031fe
Bounds-check stringref copies (which are safe anyway)
Ethan Blanton <elb@pidgin.im>
parents:
22351
diff
changeset
|
74 | |
|
7d8e1af031fe
Bounds-check stringref copies (which are safe anyway)
Ethan Blanton <elb@pidgin.im>
parents:
22351
diff
changeset
|
75 | newref = g_malloc(sizeof(PurpleStringref) + len); |
|
33594
ef3d115642d7
Fix purple_stringref_new()
Richard Laager <rlaager@pidgin.im>
parents:
32024
diff
changeset
|
76 | /* g_strlcpy() takes the size of the buffer, including the NUL. |
|
ef3d115642d7
Fix purple_stringref_new()
Richard Laager <rlaager@pidgin.im>
parents:
32024
diff
changeset
|
77 | strlen() returns the length of the string, without the NUL. */ |
|
ef3d115642d7
Fix purple_stringref_new()
Richard Laager <rlaager@pidgin.im>
parents:
32024
diff
changeset
|
78 | g_strlcpy(newref->value, value, len + 1); |
| 7763 | 79 | newref->ref = 1; |
| 80 | ||
| 81 | return newref; | |
| 82 | } | |
| 83 | ||
| 15884 | 84 | PurpleStringref *purple_stringref_new_noref(const char *value) |
| 7786 | 85 | { |
| 15884 | 86 | PurpleStringref *newref; |
| 7786 | 87 | |
| 88 | if (value == NULL) | |
| 89 | return NULL; | |
| 90 | ||
| 15884 | 91 | newref = g_malloc(sizeof(PurpleStringref) + strlen(value)); |
| 7786 | 92 | strcpy(newref->value, value); |
| 93 | newref->ref = 0x80000000; | |
| 94 | ||
| 95 | if (gclist == NULL) | |
|
22351
6ca0640b3d31
Change some g_idle_add(...) calls in libpurple to purple_timeout_add(0, ...)
Mark Doliner <markdoliner@pidgin.im>
parents:
20147
diff
changeset
|
96 | purple_timeout_add(0, gs_idle_cb, NULL); |
| 7786 | 97 | gclist = g_list_prepend(gclist, newref); |
| 98 | ||
| 99 | return newref; | |
| 100 | } | |
| 101 | ||
| 15884 | 102 | PurpleStringref *purple_stringref_printf(const char *format, ...) |
| 7767 | 103 | { |
| 15884 | 104 | PurpleStringref *newref; |
| 7767 | 105 | va_list ap; |
| 106 | ||
| 107 | if (format == NULL) | |
| 108 | return NULL; | |
| 109 | ||
| 110 | va_start(ap, format); | |
| 15884 | 111 | newref = g_malloc(sizeof(PurpleStringref) + g_printf_string_upper_bound(format, ap)); |
| 7767 | 112 | vsprintf(newref->value, format, ap); |
| 113 | va_end(ap); | |
| 7784 | 114 | newref->ref = 1; |
| 7767 | 115 | |
| 116 | return newref; | |
| 117 | } | |
| 118 | ||
| 15884 | 119 | PurpleStringref *purple_stringref_ref(PurpleStringref *stringref) |
| 7763 | 120 | { |
| 121 | if (stringref == NULL) | |
| 122 | return NULL; | |
| 123 | stringref->ref++; | |
| 124 | return stringref; | |
| 125 | } | |
| 126 | ||
| 15884 | 127 | void purple_stringref_unref(PurpleStringref *stringref) |
| 7763 | 128 | { |
| 7790 | 129 | if (stringref == NULL) |
| 130 | return; | |
| 7795 | 131 | if (REFCOUNT(--(stringref->ref)) == 0) { |
| 7786 | 132 | if (stringref->ref & 0x80000000) |
| 133 | gclist = g_list_remove(gclist, stringref); | |
| 7788 | 134 | stringref_free(stringref); |
| 7786 | 135 | } |
| 7763 | 136 | } |
| 137 | ||
| 15884 | 138 | const char *purple_stringref_value(const PurpleStringref *stringref) |
| 7763 | 139 | { |
| 140 | return (stringref == NULL ? NULL : stringref->value); | |
| 141 | } | |
| 7786 | 142 | |
| 15884 | 143 | int purple_stringref_cmp(const PurpleStringref *s1, const PurpleStringref *s2) |
| 7786 | 144 | { |
| 15884 | 145 | return (s1 == s2 ? 0 : strcmp(purple_stringref_value(s1), purple_stringref_value(s2))); |
| 7786 | 146 | } |
| 147 | ||
| 15884 | 148 | size_t purple_stringref_len(const PurpleStringref *stringref) |
| 7786 | 149 | { |
| 15884 | 150 | return strlen(purple_stringref_value(stringref)); |
| 7786 | 151 | } |
| 152 | ||
| 15884 | 153 | static void stringref_free(PurpleStringref *stringref) |
| 7786 | 154 | { |
| 155 | #ifdef DEBUG | |
| 7795 | 156 | if (REFCOUNT(stringref->ref) != 0) { |
| 15884 | 157 | purple_debug(PURPLE_DEBUG_ERROR, "stringref", "Free of nonzero (%d) ref stringref!\n", REFCOUNT(stringref->ref)); |
| 7786 | 158 | return; |
| 159 | } | |
| 160 | #endif /* DEBUG */ | |
| 161 | g_free(stringref); | |
| 162 | } | |
| 163 | ||
| 164 | static gboolean gs_idle_cb(gpointer data) | |
| 165 | { | |
| 15884 | 166 | PurpleStringref *ref; |
| 7786 | 167 | GList *del; |
| 168 | ||
| 169 | while (gclist != NULL) { | |
| 170 | ref = gclist->data; | |
| 7795 | 171 | if (REFCOUNT(ref->ref) == 0) { |
| 7786 | 172 | stringref_free(ref); |
| 173 | } | |
| 174 | del = gclist; | |
| 175 | gclist = gclist->next; | |
| 176 | g_list_free_1(del); | |
| 177 | } | |
| 178 | ||
| 179 | return FALSE; | |
| 180 | } |