From a8ff1b4fcef1e2c53603b30b918304f7be9f27b4 Mon Sep 17 00:00:00 2001 From: Tim Janik Date: Wed, 10 Feb 1999 08:06:26 +0000 Subject: [PATCH] fixed errernerous code wrt to thread specific error string allocation Wed Feb 10 07:56:33 1999 Tim Janik * gmodule.c (g_module_error): fixed errernerous code wrt to thread specific error string allocation handling. Wed Feb 10 06:20:30 1999 Tim Janik * gmutex.c (g_static_private_set): invoke destroy notifier when overwriting values, initialize new array fields with NULL. (g_static_private_free_data): do not skip destroy notification for data == NULL. * gutils.c (g_direct_equal): compare pointer values directly instead of just their guint values which is a loosing conversion for sizeof(gpointer)==8 systems. (g_get_any_init): restructured code so we don't use endless loops like while (1), which boil down to an ugly alias for goto. strip ,.* from the real name. --- ChangeLog | 14 ++++++++ ChangeLog.pre-2-0 | 14 ++++++++ ChangeLog.pre-2-10 | 14 ++++++++ ChangeLog.pre-2-12 | 14 ++++++++ ChangeLog.pre-2-2 | 14 ++++++++ ChangeLog.pre-2-4 | 14 ++++++++ ChangeLog.pre-2-6 | 14 ++++++++ ChangeLog.pre-2-8 | 14 ++++++++ glib.h | 10 +++--- glib/glib.h | 10 +++--- glib/gstrfuncs.c | 10 +++--- glib/gutils.c | 84 +++++++++++++++++++++++++++------------------- gmodule/ChangeLog | 5 +++ gmodule/gmodule.c | 13 ++----- gmutex.c | 35 ++++++++++++++----- gstrfuncs.c | 10 +++--- gutils.c | 84 +++++++++++++++++++++++++++------------------- 17 files changed, 269 insertions(+), 104 deletions(-) diff --git a/ChangeLog b/ChangeLog index 842fe1563..16cdd2cb3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Wed Feb 10 06:20:30 1999 Tim Janik + + * gmutex.c (g_static_private_set): invoke destroy notifier when + overwriting values, initialize new array fields with NULL. + (g_static_private_free_data): do not skip destroy notification for + data == NULL. + + * gutils.c (g_direct_equal): compare pointer values directly instead + of just their guint values which is a loosing conversion for + sizeof(gpointer)==8 systems. + (g_get_any_init): restructured code so we don't use endless loops like + while (1), which boil down to an ugly alias for goto. + strip ,.* from the real name. + February 9, 1999 sopwith@redhat.com . glist.c, gnode.c, gslist.c: allocate pools of 128 items instead diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 842fe1563..16cdd2cb3 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,17 @@ +Wed Feb 10 06:20:30 1999 Tim Janik + + * gmutex.c (g_static_private_set): invoke destroy notifier when + overwriting values, initialize new array fields with NULL. + (g_static_private_free_data): do not skip destroy notification for + data == NULL. + + * gutils.c (g_direct_equal): compare pointer values directly instead + of just their guint values which is a loosing conversion for + sizeof(gpointer)==8 systems. + (g_get_any_init): restructured code so we don't use endless loops like + while (1), which boil down to an ugly alias for goto. + strip ,.* from the real name. + February 9, 1999 sopwith@redhat.com . glist.c, gnode.c, gslist.c: allocate pools of 128 items instead diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 842fe1563..16cdd2cb3 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,17 @@ +Wed Feb 10 06:20:30 1999 Tim Janik + + * gmutex.c (g_static_private_set): invoke destroy notifier when + overwriting values, initialize new array fields with NULL. + (g_static_private_free_data): do not skip destroy notification for + data == NULL. + + * gutils.c (g_direct_equal): compare pointer values directly instead + of just their guint values which is a loosing conversion for + sizeof(gpointer)==8 systems. + (g_get_any_init): restructured code so we don't use endless loops like + while (1), which boil down to an ugly alias for goto. + strip ,.* from the real name. + February 9, 1999 sopwith@redhat.com . glist.c, gnode.c, gslist.c: allocate pools of 128 items instead diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 842fe1563..16cdd2cb3 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,17 @@ +Wed Feb 10 06:20:30 1999 Tim Janik + + * gmutex.c (g_static_private_set): invoke destroy notifier when + overwriting values, initialize new array fields with NULL. + (g_static_private_free_data): do not skip destroy notification for + data == NULL. + + * gutils.c (g_direct_equal): compare pointer values directly instead + of just their guint values which is a loosing conversion for + sizeof(gpointer)==8 systems. + (g_get_any_init): restructured code so we don't use endless loops like + while (1), which boil down to an ugly alias for goto. + strip ,.* from the real name. + February 9, 1999 sopwith@redhat.com . glist.c, gnode.c, gslist.c: allocate pools of 128 items instead diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 842fe1563..16cdd2cb3 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,17 @@ +Wed Feb 10 06:20:30 1999 Tim Janik + + * gmutex.c (g_static_private_set): invoke destroy notifier when + overwriting values, initialize new array fields with NULL. + (g_static_private_free_data): do not skip destroy notification for + data == NULL. + + * gutils.c (g_direct_equal): compare pointer values directly instead + of just their guint values which is a loosing conversion for + sizeof(gpointer)==8 systems. + (g_get_any_init): restructured code so we don't use endless loops like + while (1), which boil down to an ugly alias for goto. + strip ,.* from the real name. + February 9, 1999 sopwith@redhat.com . glist.c, gnode.c, gslist.c: allocate pools of 128 items instead diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 842fe1563..16cdd2cb3 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,17 @@ +Wed Feb 10 06:20:30 1999 Tim Janik + + * gmutex.c (g_static_private_set): invoke destroy notifier when + overwriting values, initialize new array fields with NULL. + (g_static_private_free_data): do not skip destroy notification for + data == NULL. + + * gutils.c (g_direct_equal): compare pointer values directly instead + of just their guint values which is a loosing conversion for + sizeof(gpointer)==8 systems. + (g_get_any_init): restructured code so we don't use endless loops like + while (1), which boil down to an ugly alias for goto. + strip ,.* from the real name. + February 9, 1999 sopwith@redhat.com . glist.c, gnode.c, gslist.c: allocate pools of 128 items instead diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 842fe1563..16cdd2cb3 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,17 @@ +Wed Feb 10 06:20:30 1999 Tim Janik + + * gmutex.c (g_static_private_set): invoke destroy notifier when + overwriting values, initialize new array fields with NULL. + (g_static_private_free_data): do not skip destroy notification for + data == NULL. + + * gutils.c (g_direct_equal): compare pointer values directly instead + of just their guint values which is a loosing conversion for + sizeof(gpointer)==8 systems. + (g_get_any_init): restructured code so we don't use endless loops like + while (1), which boil down to an ugly alias for goto. + strip ,.* from the real name. + February 9, 1999 sopwith@redhat.com . glist.c, gnode.c, gslist.c: allocate pools of 128 items instead diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 842fe1563..16cdd2cb3 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,17 @@ +Wed Feb 10 06:20:30 1999 Tim Janik + + * gmutex.c (g_static_private_set): invoke destroy notifier when + overwriting values, initialize new array fields with NULL. + (g_static_private_free_data): do not skip destroy notification for + data == NULL. + + * gutils.c (g_direct_equal): compare pointer values directly instead + of just their guint values which is a loosing conversion for + sizeof(gpointer)==8 systems. + (g_get_any_init): restructured code so we don't use endless loops like + while (1), which boil down to an ugly alias for goto. + strip ,.* from the real name. + February 9, 1999 sopwith@redhat.com . glist.c, gnode.c, gslist.c: allocate pools of 128 items instead diff --git a/glib.h b/glib.h index a50f6d0f3..46f1465f8 100644 --- a/glib.h +++ b/glib.h @@ -1632,10 +1632,10 @@ void g_string_sprintfa (GString *string, * order by moving the last element to the position of the removed */ -#define g_array_append_val(a,v) g_array_append_vals(a,&v,1) -#define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1) -#define g_array_insert_val(a,i,v) g_array_insert_vals(a,i,&v,1) -#define g_array_index(a,t,i) (((t*)a->data)[i]) +#define g_array_append_val(a,v) g_array_append_vals (a, &v, 1) +#define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1) +#define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1) +#define g_array_index(a,t,i) (((t*) (a)->data) [(i)]) GArray* g_array_new (gboolean zero_terminated, gboolean clear, @@ -1715,6 +1715,8 @@ guint g_int_hash (gconstpointer v); /* This "hash" function will just return the key's adress as an * unsigned integer. Useful for hashing on plain adresses or * simple integer values. + * passing NULL into g_hash_table_new() as GHashFunc has the + * same effect as passing g_direct_hash(). */ guint g_direct_hash (gconstpointer v); gint g_direct_equal (gconstpointer v, diff --git a/glib/glib.h b/glib/glib.h index a50f6d0f3..46f1465f8 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -1632,10 +1632,10 @@ void g_string_sprintfa (GString *string, * order by moving the last element to the position of the removed */ -#define g_array_append_val(a,v) g_array_append_vals(a,&v,1) -#define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1) -#define g_array_insert_val(a,i,v) g_array_insert_vals(a,i,&v,1) -#define g_array_index(a,t,i) (((t*)a->data)[i]) +#define g_array_append_val(a,v) g_array_append_vals (a, &v, 1) +#define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1) +#define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1) +#define g_array_index(a,t,i) (((t*) (a)->data) [(i)]) GArray* g_array_new (gboolean zero_terminated, gboolean clear, @@ -1715,6 +1715,8 @@ guint g_int_hash (gconstpointer v); /* This "hash" function will just return the key's adress as an * unsigned integer. Useful for hashing on plain adresses or * simple integer values. + * passing NULL into g_hash_table_new() as GHashFunc has the + * same effect as passing g_direct_hash(). */ guint g_direct_hash (gconstpointer v); gint g_direct_equal (gconstpointer v, diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 0a1648515..32fdeb52f 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -644,13 +644,14 @@ g_strerror (gint errnum) #endif /* NO_SYS_ERRLIST */ msg = g_static_private_get (&msg_private); - if( !msg ) + if (!msg) { - msg = g_new( gchar, 64 ); + msg = g_new (gchar, 64); g_static_private_set (&msg_private, msg, g_free); } sprintf (msg, "unknown error (%d)", errnum); + return msg; } @@ -770,13 +771,14 @@ g_strsignal (gint signum) #endif /* NO_SYS_SIGLIST */ msg = g_static_private_get (&msg_private); - if( !msg ) + if (!msg) { - msg = g_new( gchar, 64 ); + msg = g_new (gchar, 64); g_static_private_set (&msg_private, msg, g_free); } sprintf (msg, "unknown signal (%d)", signum); + return msg; } diff --git a/glib/gutils.c b/glib/gutils.c index 04771cc79..8e4d1f9aa 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -401,7 +401,7 @@ g_get_any_init (void) g_tmp_dir[k-1] = '\0'; } #endif - + if (!g_tmp_dir) { #ifndef NATIVE_WIN32 @@ -442,18 +442,18 @@ g_get_any_init (void) { struct passwd *pw = NULL; gpointer buffer = NULL; - + # ifdef HAVE_GETPWUID_R struct passwd pwd; guint bufsize = 64; gint error; - - while (1) + + do { g_free (buffer); buffer = g_malloc (bufsize); errno = 0; - + # ifdef HAVE_GETPWUID_R_POSIX error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw); error = error < 0 ? errno : error; @@ -461,33 +461,33 @@ g_get_any_init (void) pw = getpwuid_r (getuid (), &pwd, buffer, bufsize); error = pw ? 0 : errno; # endif /* !HAVE_GETPWUID_R_POSIX */ - - /* Now there are actually only 3 cases to leave the loop: - 1. pw != NULL -> all went fine. - 2. pw == NULL && ( error == 0 || error == ENOENT ) - -> no such user (unlikely in the case of getuid ()) - 3. bufsize > 32k -> the problem can't be of ERANGE type */ - if (pw) - break; - - if (pw == NULL && ( error == 0 || error == ENOENT)) + + if (!pw) { - g_warning ("getpwuid_r(): failed due to: No such user %d.", - getuid ()); - break; + /* we bail out prematurely if the user id can't be found + * (should be pretty rare case actually), or if the buffer + * should be sufficiently big and lookups are still not + * successfull. + */ + if (error == 0 || error == ENOENT) + { + g_warning ("getpwuid_r(): failed due to: No such user %d.", + getuid ()); + break; + } + if (bufsize > 32 * 1024) + { + g_warning ("getpwuid_r(): failed due to: %s.", + g_strerror (error)); + break; + } + + bufsize *= 2; } - - if (bufsize > 32 * 1024) - { - g_warning ("getpwuid_r(): failed due to: %s.", - g_strerror (error)); - break; - } - - bufsize *= 2; - } + } + while (!pw); # endif /* !HAVE_GETPWUID_R */ - + if (!pw) { setpwent (); @@ -503,9 +503,9 @@ g_get_any_init (void) } g_free (buffer); } - + #else /* !HAVE_PWD_H */ - + # ifdef NATIVE_WIN32 { guint len = 17; @@ -518,14 +518,28 @@ g_get_any_init (void) } } # endif /* NATIVE_WIN32 */ - + #endif /* !HAVE_PWD_H */ - + if (!g_user_name) g_user_name = g_strdup ("somebody"); if (!g_real_name) g_real_name = g_strdup ("Unknown"); - } + else + { + gchar *p; + + for (p = g_real_name; *p; p++) + if (*p == ',') + { + *p = 0; + p = g_strdup (g_real_name); + g_free (g_real_name); + g_real_name = p; + break; + } + } + } } gchar* @@ -621,7 +635,7 @@ gint g_direct_equal (gconstpointer v1, gconstpointer v2) { - return GPOINTER_TO_UINT (v1) == GPOINTER_TO_UINT (v2); + return v1 == v2; } gint diff --git a/gmodule/ChangeLog b/gmodule/ChangeLog index c957056a7..74cf3ae8e 100644 --- a/gmodule/ChangeLog +++ b/gmodule/ChangeLog @@ -1,3 +1,8 @@ +Wed Feb 10 07:56:33 1999 Tim Janik + + * gmodule.c (g_module_error): fixed errernerous code wrt to thread + specific error string allocation handling. + Thu Jan 21 12:40:11 EST 1999 Jeff Garzik * gmodule-dl.c (_g_module_build_path): diff --git a/gmodule/gmodule.c b/gmodule/gmodule.c index 99f38a6a4..463bcb16a 100644 --- a/gmodule/gmodule.c +++ b/gmodule/gmodule.c @@ -114,15 +114,8 @@ g_module_find_by_name (const gchar *name) static inline void g_module_set_error (const gchar *error) { - gchar* module_error = g_static_private_get (&module_error_private); - if (module_error) - g_free (module_error); - if (error) - module_error = g_strdup (error); - else - module_error = NULL; + g_static_private_set (&module_error_private, g_strdup (error), g_free); errno = 0; - g_static_private_set (&module_error_private, module_error, g_free); } @@ -246,8 +239,7 @@ g_module_open (const gchar *file_name, return module; } - saved_error = g_module_error(); - g_static_private_set (&module_error_private, NULL, NULL); + saved_error = g_strdup (g_module_error ()); g_module_set_error (NULL); module = g_new (GModule, 1); @@ -281,6 +273,7 @@ g_module_open (const gchar *file_name, } else g_module_set_error (saved_error); + g_free (saved_error); } diff --git a/gmutex.c b/gmutex.c index dfcad9d2f..f2e3e48fa 100644 --- a/gmutex.c +++ b/gmutex.c @@ -29,7 +29,8 @@ typedef struct _GStaticPrivateNode GStaticPrivateNode; -struct _GStaticPrivateNode { +struct _GStaticPrivateNode +{ gpointer data; GDestroyNotify destroy; }; @@ -72,12 +73,15 @@ void g_mutex_init (void) { /* We let the main thread (the one that calls g_thread_init) inherit - the data, that it set before calling g_thread_init */ + * the data, that it set before calling g_thread_init + */ gpointer private_old = g_thread_specific_private; + g_thread_specific_private = g_private_new (g_static_private_free_data); /* we can not use g_private_set here, as g_threads_got_initialized is not - yet set TRUE, whereas the private_set function is already set. */ + * yet set TRUE, whereas the private_set function is already set. + */ g_thread_functions_for_glib_use.private_set (g_thread_specific_private, private_old); @@ -115,7 +119,7 @@ g_static_private_get (GStaticPrivate *private_key) if (!private_key->index) return NULL; else if (private_key->index <= array->len) - return g_array_index (array, GStaticPrivateNode, (private_key->index - 1)).data; + return g_array_index (array, GStaticPrivateNode, private_key->index - 1).data; else return NULL; } @@ -127,11 +131,12 @@ g_static_private_set (GStaticPrivate *private_key, { GArray *array; static guint next_index = 0; + GStaticPrivateNode *node; array = g_private_get (g_thread_specific_private); if (!array) { - array = g_array_new (FALSE, FALSE, sizeof(GStaticPrivateNode)); + array = g_array_new (FALSE, TRUE, sizeof (GStaticPrivateNode)); g_private_set (g_thread_specific_private, array); } @@ -148,8 +153,22 @@ g_static_private_set (GStaticPrivate *private_key, if (private_key->index > array->len) g_array_set_size (array, private_key->index); - g_array_index (array, GStaticPrivateNode, (private_key->index - 1)).data = data; - g_array_index (array, GStaticPrivateNode, (private_key->index - 1)).destroy = notify; + node = &g_array_index (array, GStaticPrivateNode, private_key->index - 1); + if (node->destroy) + { + gpointer ddata = node->data; + GDestroyNotify ddestroy = node->destroy; + + node->data = data; + node->destroy = notify; + + ddestroy (ddata); + } + else + { + node->data = data; + node->destroy = notify; + } } static void @@ -163,7 +182,7 @@ g_static_private_free_data (gpointer data) for (i = 0; i < array->len; i++ ) { GStaticPrivateNode *node = &g_array_index (array, GStaticPrivateNode, i); - if (node->data && node->destroy) + if (node->destroy) node->destroy (node->data); } } diff --git a/gstrfuncs.c b/gstrfuncs.c index 0a1648515..32fdeb52f 100644 --- a/gstrfuncs.c +++ b/gstrfuncs.c @@ -644,13 +644,14 @@ g_strerror (gint errnum) #endif /* NO_SYS_ERRLIST */ msg = g_static_private_get (&msg_private); - if( !msg ) + if (!msg) { - msg = g_new( gchar, 64 ); + msg = g_new (gchar, 64); g_static_private_set (&msg_private, msg, g_free); } sprintf (msg, "unknown error (%d)", errnum); + return msg; } @@ -770,13 +771,14 @@ g_strsignal (gint signum) #endif /* NO_SYS_SIGLIST */ msg = g_static_private_get (&msg_private); - if( !msg ) + if (!msg) { - msg = g_new( gchar, 64 ); + msg = g_new (gchar, 64); g_static_private_set (&msg_private, msg, g_free); } sprintf (msg, "unknown signal (%d)", signum); + return msg; } diff --git a/gutils.c b/gutils.c index 04771cc79..8e4d1f9aa 100644 --- a/gutils.c +++ b/gutils.c @@ -401,7 +401,7 @@ g_get_any_init (void) g_tmp_dir[k-1] = '\0'; } #endif - + if (!g_tmp_dir) { #ifndef NATIVE_WIN32 @@ -442,18 +442,18 @@ g_get_any_init (void) { struct passwd *pw = NULL; gpointer buffer = NULL; - + # ifdef HAVE_GETPWUID_R struct passwd pwd; guint bufsize = 64; gint error; - - while (1) + + do { g_free (buffer); buffer = g_malloc (bufsize); errno = 0; - + # ifdef HAVE_GETPWUID_R_POSIX error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw); error = error < 0 ? errno : error; @@ -461,33 +461,33 @@ g_get_any_init (void) pw = getpwuid_r (getuid (), &pwd, buffer, bufsize); error = pw ? 0 : errno; # endif /* !HAVE_GETPWUID_R_POSIX */ - - /* Now there are actually only 3 cases to leave the loop: - 1. pw != NULL -> all went fine. - 2. pw == NULL && ( error == 0 || error == ENOENT ) - -> no such user (unlikely in the case of getuid ()) - 3. bufsize > 32k -> the problem can't be of ERANGE type */ - if (pw) - break; - - if (pw == NULL && ( error == 0 || error == ENOENT)) + + if (!pw) { - g_warning ("getpwuid_r(): failed due to: No such user %d.", - getuid ()); - break; + /* we bail out prematurely if the user id can't be found + * (should be pretty rare case actually), or if the buffer + * should be sufficiently big and lookups are still not + * successfull. + */ + if (error == 0 || error == ENOENT) + { + g_warning ("getpwuid_r(): failed due to: No such user %d.", + getuid ()); + break; + } + if (bufsize > 32 * 1024) + { + g_warning ("getpwuid_r(): failed due to: %s.", + g_strerror (error)); + break; + } + + bufsize *= 2; } - - if (bufsize > 32 * 1024) - { - g_warning ("getpwuid_r(): failed due to: %s.", - g_strerror (error)); - break; - } - - bufsize *= 2; - } + } + while (!pw); # endif /* !HAVE_GETPWUID_R */ - + if (!pw) { setpwent (); @@ -503,9 +503,9 @@ g_get_any_init (void) } g_free (buffer); } - + #else /* !HAVE_PWD_H */ - + # ifdef NATIVE_WIN32 { guint len = 17; @@ -518,14 +518,28 @@ g_get_any_init (void) } } # endif /* NATIVE_WIN32 */ - + #endif /* !HAVE_PWD_H */ - + if (!g_user_name) g_user_name = g_strdup ("somebody"); if (!g_real_name) g_real_name = g_strdup ("Unknown"); - } + else + { + gchar *p; + + for (p = g_real_name; *p; p++) + if (*p == ',') + { + *p = 0; + p = g_strdup (g_real_name); + g_free (g_real_name); + g_real_name = p; + break; + } + } + } } gchar* @@ -621,7 +635,7 @@ gint g_direct_equal (gconstpointer v1, gconstpointer v2) { - return GPOINTER_TO_UINT (v1) == GPOINTER_TO_UINT (v2); + return v1 == v2; } gint