diff --git a/Makefile b/Makefile index bec6f4a..a9bb755 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ LIBS := `pkg-config --libs libxml-2.0` \ # common source files SRCS := src/conf.c \ + src/mem.c \ src/log.c \ src/xpath.c \ src/hal.c \ diff --git a/src/conf.c b/src/conf.c index 70d7837..0d8f101 100644 --- a/src/conf.c +++ b/src/conf.c @@ -18,6 +18,7 @@ #include #include #include +#include "mem.h" #include "conf.h" #include "xpath.h" #include "log.h" @@ -71,15 +72,11 @@ static int pusb_conf_parse_options(t_pusb_options *opts, for (i = 0; opt_list[i].name != NULL; ++i) { xpath_size = strlen(opt_list[i].name) + strlen(opt_list[i].value) + 1; - if (!(xpath = malloc(xpath_size))) - { - log_error("malloc error\n"); - return (0); - } + xpath = xmalloc(xpath_size); memset(xpath, 0x00, xpath_size); snprintf(xpath, xpath_size, opt_list[i].name, opt_list[i].value, ""); pusb_conf_options_get_from(opts, xpath, doc); - free(xpath); + xfree(xpath); } return (1); } @@ -96,16 +93,12 @@ static int pusb_conf_device_get_property(t_pusb_options *opts, xpath_len = strlen(CONF_DEVICE_XPATH) + strlen(opts->device.name) + \ strlen(property) + 1; - if (!(xpath = malloc(xpath_len))) - { - log_error("malloc error!\n"); - return (0); - } + xpath = xmalloc(xpath_len); memset(xpath, 0x00, xpath_len); snprintf(xpath, xpath_len, CONF_DEVICE_XPATH, opts->device.name, property); retval = pusb_xpath_get_string(doc, xpath, store, size); - free(xpath); + xfree(xpath); return (retval); } diff --git a/src/device.c b/src/device.c index 04a5ad9..dfe9ae0 100644 --- a/src/device.c +++ b/src/device.c @@ -19,6 +19,7 @@ #include #include #include +#include "mem.h" #include "conf.h" #include "hal.h" #include "log.h" @@ -42,7 +43,7 @@ static int pusb_device_connected(t_pusb_options *opts, DBusConnection *dbus) opts->device.name); return (0); } - free(udi); + xfree(udi); log_info("Device \"%s\" is connected (good).\n", opts->device.name); return (1); } diff --git a/src/hal.c b/src/hal.c index c9da72f..f951792 100644 --- a/src/hal.c +++ b/src/hal.c @@ -20,6 +20,7 @@ #include #include #include +#include "mem.h" #include "log.h" #include "hal.h" @@ -66,8 +67,8 @@ void pusb_hal_free_string_array(char **str_array, int length) return ; for (i = 0; i < length; ++i) - free(str_array[i]); - free(str_array); + xfree(str_array[i]); + xfree(str_array); } char **pusb_hal_get_string_array_from_iter(DBusMessageIter *iter, int *num_elements) @@ -76,7 +77,7 @@ char **pusb_hal_get_string_array_from_iter(DBusMessageIter *iter, int *num_eleme char **buffer; count = 0; - buffer = (char **)malloc(sizeof(char *) * 8); + buffer = (char **)xmalloc(sizeof(char *) * 8); buffer[0] = NULL; while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING || @@ -85,11 +86,11 @@ char **pusb_hal_get_string_array_from_iter(DBusMessageIter *iter, int *num_eleme const char *value; if ((count % 8) == 0 && count != 0) { - buffer = realloc(buffer, sizeof (char *) * (count + 8)); + buffer = xrealloc(buffer, sizeof (char *) * (count + 8)); } dbus_message_iter_get_basic(iter, &value); - buffer[count] = strdup(value); + buffer[count] = xstrdup(value); dbus_message_iter_next(iter); count++; @@ -162,7 +163,7 @@ char *pusb_hal_get_string_property(DBusConnection *dbus, dbus_message_iter_recurse(&reply_iter, &subiter); dbus_message_iter_get_basic(&subiter, &dbus_str); if (dbus_str != NULL) - data = strdup(dbus_str); + data = xstrdup(dbus_str); dbus_message_unref(reply); return (data); } @@ -245,7 +246,7 @@ int pusb_hal_check_property(DBusConnection *dbus, if (!data) return (0); retval = (strcmp(data, value) == 0); - free(data); + xfree(data); return (retval); } @@ -334,7 +335,7 @@ char *pusb_hal_find_item(DBusConnection *dbus, } if (match) { - udi = strdup(devices[i]); + udi = xstrdup(devices[i]); break; } va_end(ap); diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 0000000..e76f666 --- /dev/null +++ b/src/mem.c @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2003-2007 Andrea Luzzardi + * + * This file is part of the pam_usb project. pam_usb is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * pam_usb is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include "mem.h" + +void *xmalloc(size_t size) +{ + void *data = malloc(size); + assert(data != NULL && "malloc() failed"); + return (data); +} + +void *xrealloc(void *ptr, size_t size) +{ + void *data = realloc(ptr, size); + assert(data != NULL && "realloc() failed"); + return (data); +} + +void xfree(void *ptr) +{ + free(ptr); +} + +char *xstrdup(const char *s) +{ + char *data = strdup(s); + assert(data != NULL && "strdup() failed"); + return (data); +} diff --git a/src/mem.h b/src/mem.h new file mode 100644 index 0000000..b5fbd0d --- /dev/null +++ b/src/mem.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2003-2007 Andrea Luzzardi + * + * This file is part of the pam_usb project. pam_usb is free software; + * you can redistribute it and/or modify it under the terms of the GNU General + * Public License version 2, as published by the Free Software Foundation. + * + * pam_usb is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PUSB_MEM_H_ +# define PUSB_MEM_H_ +# include +# include + +void *xmalloc(size_t size); +void *xrealloc(void *ptr, size_t size); +void xfree(void *ptr); +char *xstrdup(const char *s); + +#endif /* !PUSB_MEM_H_ */ diff --git a/src/volume.c b/src/volume.c index 9e69f1d..d54ee72 100644 --- a/src/volume.c +++ b/src/volume.c @@ -22,6 +22,7 @@ #include #include #include +#include "mem.h" #include "conf.h" #include "log.h" #include "hal.h" @@ -79,7 +80,7 @@ static char *pusb_volume_mount_path(t_pusb_options *opts, char *udi, DBusConnect { log_debug("Device %s is mounted more than once\n", udi); } - char *mount_path = strdup(mount_pathes[0]); + char *mount_path = xstrdup(mount_pathes[0]); pusb_hal_free_string_array(mount_pathes, n_mount); log_debug("Device %s is mounted on %s\n", udi, mount_path); return (mount_path); @@ -133,7 +134,7 @@ char *pusb_volume_get(t_pusb_options *opts, DBusConnection *dbus) } if (!pusb_volume_mount(opts, volume_udi, dbus)) { - free(volume_udi); + xfree(volume_udi); return (NULL); } mount_point = pusb_volume_mount_path(opts, volume_udi, dbus); @@ -160,5 +161,5 @@ void pusb_volume_destroy(char *mntpoint) else log_error("Unable to umount %s\n", mntpoint); } - free(mntpoint); + xfree(mntpoint); } diff --git a/src/xpath.c b/src/xpath.c index a1388b8..9673bea 100644 --- a/src/xpath.c +++ b/src/xpath.c @@ -18,6 +18,7 @@ #include #include #include +#include "mem.h" #include "xpath.h" #include "log.h" @@ -126,17 +127,13 @@ int pusb_xpath_get_string_from(xmlDocPtr doc, int retval; xpath_size = strlen(base) + strlen(path) + 1; - if (!(xpath = malloc(xpath_size))) - { - log_error("malloc error !\n"); - return (0); - } + xpath = xmalloc(xpath_size); memset(xpath, 0x00, xpath_size); snprintf(xpath, xpath_size, "%s%s", base, path); retval = pusb_xpath_get_string(doc, xpath, value, size); if (retval) log_debug("%s%s -> %s\n", base, path, value); - free(xpath); + xfree(xpath); return (retval); } @@ -173,15 +170,11 @@ int pusb_xpath_get_bool_from(xmlDocPtr doc, int retval; xpath_size = strlen(base) + strlen(path) + 1; - if (!(xpath = malloc(xpath_size))) - { - log_error("malloc error!\n"); - return (0); - } + xpath = xmalloc(xpath_size); memset(xpath, 0x00, xpath_size); snprintf(xpath, xpath_size, "%s%s", base, path); retval = pusb_xpath_get_bool(doc, xpath, value); - free(xpath); + xfree(xpath); return (retval); } @@ -227,15 +220,11 @@ int pusb_xpath_get_time_from(xmlDocPtr doc, int retval; xpath_size = strlen(base) + strlen(path) + 1; - if (!(xpath = malloc(xpath_size))) - { - log_error("malloc error!\n"); - return (0); - } + xpath = xmalloc(xpath_size); memset(xpath, 0x00, xpath_size); snprintf(xpath, xpath_size, "%s%s", base, path); retval = pusb_xpath_get_time(doc, xpath, value); - free(xpath); + xfree(xpath); return (retval); } @@ -259,14 +248,10 @@ int pusb_xpath_get_int_from(xmlDocPtr doc, int retval; xpath_size = strlen(base) + strlen(path) + 1; - if (!(xpath = malloc(xpath_size))) - { - log_error("malloc error!\n"); - return (0); - } + xpath = xmalloc(xpath_size); memset(xpath, 0x00, xpath_size); snprintf(xpath, xpath_size, "%s%s", base, path); retval = pusb_xpath_get_int(doc, xpath, value); - free(xpath); + xfree(xpath); return (retval); }