hal: Branch 'master' - 4 commits

Danny Kukawka dkukawka at kemper.freedesktop.org
Tue Nov 20 06:05:47 PST 2007


 hald/create_cache.c                      |    3 
 hald/linux/addons/addon-omap-backlight.c |  219 ++++++++++++++++---------------
 libhal/libhal.c                          |    2 
 partutil/partutil.c                      |   24 ++-
 4 files changed, 136 insertions(+), 112 deletions(-)

New commits:
commit 7f414a36784efa0dbef3a337518dc3cd3a1d7b4a
Author: Guillem Jover <guillem.jover at nokia.com>
Date:   Tue Nov 20 15:00:29 2007 +0100

    dereference pointer after checking it's not NULL
    
    Dereference pointer after checking it's not NULL in
    partutil/partutil.c.

diff --git a/partutil/partutil.c b/partutil/partutil.c
index 36078c0..b22f9b2 100644
--- a/partutil/partutil.c
+++ b/partutil/partutil.c
@@ -928,11 +928,13 @@ char *
 part_table_entry_get_type (PartitionTable *p, int entry)
 {
 	char *s = NULL;
-	PartitionEntry *pe = g_slist_nth_data (p->entries, entry);
+	PartitionEntry *pe;
 
 	if (p == NULL)
 		goto out;
 
+	pe = g_slist_nth_data (p->entries, entry);
+
 	switch (p->scheme) {
 	case PART_TYPE_GPT:
 		s = get_le_guid (&(pe->data[0]));
@@ -959,11 +961,13 @@ char *
 part_table_entry_get_uuid (PartitionTable *p, int entry)
 {
 	char *s = NULL;
-	PartitionEntry *pe = g_slist_nth_data (p->entries, entry);
+	PartitionEntry *pe;
 
 	if (p == NULL)
 		goto out;
 
+	pe = g_slist_nth_data (p->entries, entry);
+
 	switch (p->scheme) {
 	case PART_TYPE_GPT:
 		s = get_le_guid (&(pe->data[16]));
@@ -982,11 +986,13 @@ char *
 part_table_entry_get_label (PartitionTable *p, int entry)
 {
 	char *s = NULL;
-	PartitionEntry *pe = g_slist_nth_data (p->entries, entry);
+	PartitionEntry *pe;
 
 	if (p == NULL)
 		goto out;
 
+	pe = g_slist_nth_data (p->entries, entry);
+
 	switch (p->scheme) {
 	case PART_TYPE_GPT:
 		s = g_utf16_to_utf8 ((const gunichar2 *) &(pe->data[56]), 36, NULL, NULL, NULL);
@@ -1012,11 +1018,13 @@ part_table_entry_get_flags (PartitionTable *p, int entry)
 	char **ss = NULL;
 	guint32 apm_status;
 	guint64 gpt_attributes;
-	PartitionEntry *pe = g_slist_nth_data (p->entries, entry);
+	PartitionEntry *pe;
 
 	if (p == NULL)
 		goto out;
 
+	pe = g_slist_nth_data (p->entries, entry);
+
 	ss = g_new0 (char*, 6 + 1); /* hard coded to max items we'll return */
 	ss[0] = NULL;
 	n = 0;
@@ -1087,12 +1095,14 @@ guint64
 part_table_entry_get_offset (PartitionTable *p, int entry)
 {
 	guint64 val;
-	PartitionEntry *pe = g_slist_nth_data (p->entries, entry);
+	PartitionEntry *pe;
 
 	val = G_MAXUINT64;
 	if (p == NULL)
 		goto out;
 
+	pe = g_slist_nth_data (p->entries, entry);
+
 	switch (p->scheme) {
 	case PART_TYPE_GPT:
 		val = 0x200 * ((guint64) get_le64 (pe->data + 32));
@@ -1122,12 +1132,14 @@ guint64
 part_table_entry_get_size (PartitionTable *p, int entry)
 {
 	guint64 val;
-	PartitionEntry *pe = g_slist_nth_data (p->entries, entry);
+	PartitionEntry *pe;
 
 	val = G_MAXUINT64;
 	if (p == NULL)
 		goto out;
 
+	pe = g_slist_nth_data (p->entries, entry);
+
 	switch (p->scheme) {
 	case PART_TYPE_GPT:
 		val = 0x200 * (((guint64) get_le64 (pe->data + 40)) - ((guint64) get_le64 (pe->data + 32)) + 1);
commit 522f8bede85239dc0279a94d0b1e9c1da4e601f6
Author: Guillem Jover <guillem.jover at nokia.com>
Date:   Tue Nov 20 14:50:43 2007 +0100

    libhal: do not dereference *reason_why_locked if it's NULL
    
    Do not dereference *reason_why_locked if it's NULL after call
    dbus_malloc0().

diff --git a/libhal/libhal.c b/libhal/libhal.c
index 619b7a5..2697239 100644
--- a/libhal/libhal.c
+++ b/libhal/libhal.c
@@ -2279,6 +2279,8 @@ libhal_device_lock (LibHalContext *ctx,
 			if (reason_why_locked != NULL) {
 				*reason_why_locked =
 					dbus_malloc0 (strlen (error->message) + 1);
+				if (*reason_why_locked == NULL)
+					return FALSE;
 				strcpy (*reason_why_locked, error->message);
 			}
 		}
commit 02d471d199d6a50f989cde9f452f5f70dc9f7a3d
Author: Guillem Jover <guillem.jover at nokia.com>
Date:   Tue Nov 20 14:47:35 2007 +0100

    fix file descriptor leaks on error conditions
    
    Fixed file descriptor leaks on error conditions if open()
    used.

diff --git a/hald/create_cache.c b/hald/create_cache.c
index 384ed6a..c78c03f 100644
--- a/hald/create_cache.c
+++ b/hald/create_cache.c
@@ -699,8 +699,9 @@ di_rules_init (void)
 	return num_skipped_fdi_files;
 error:
 	HAL_ERROR (("Error generating fdi cache"));
-	if (fd < 0)
+	if (fd >= 0)
 		close (fd);
+
 	unlink (cachename_temp);
 	return -1;
 }
diff --git a/hald/linux/addons/addon-omap-backlight.c b/hald/linux/addons/addon-omap-backlight.c
index b05ccbb..d85df79 100644
--- a/hald/linux/addons/addon-omap-backlight.c
+++ b/hald/linux/addons/addon-omap-backlight.c
@@ -71,13 +71,19 @@ struct backlight bl_data;
 static int
 read_backlight (struct backlight * bl)
 {
-	int fd;
+	int fd, ret;
 
 	fd = open ("/sys/devices/platform/omapfb/panel/backlight_level", O_RDONLY);
-	if (fd < 0 || read (fd, buffer, NUM_BUF_LEN) < 0)
+	if (fd < 0)
 		return -1;
 
-	return atoi (buffer);
+	ret = read (fd, buffer, NUM_BUF_LEN)
+	close (fd);
+
+	if (ret >= 0)
+		return atoi (buffer);
+	else
+		return -1;
 }
 
 /* Read maximum bl level */
@@ -88,23 +94,25 @@ read_backlight (struct backlight * bl)
 static void
 backlight_init (struct backlight * bl)
 {
-	int fd;
+	int fd, ret;
 
 	/* Reading maximum backlight level */
 	fd = open ("/sys/devices/platform/omapfb/panel/backlight_max", O_RDONLY);
-
-	if (fd < 0 || read (fd, buffer, NUM_BUF_LEN - 1) < 0)
+	if (fd < 0)
 		return;
 
-	bl->bl_max = atoi (buffer);
-	close (fd);
+	ret = read(fd, buffer, NUM_BUF_LEN - 1);
+	close(fd);
+
+	if (ret >= 0)
+		bl->bl_max = atoi (buffer);
 }
 
 /* Setting backlight level */
 static void
 write_backlight (struct backlight * bl, int level)
 {
-	int fd, l;
+	int fd, l, ret;
 
 	/* sanity-checking level we're required to set */
 	if (level > bl->bl_max)
@@ -114,14 +122,14 @@ write_backlight (struct backlight * bl, int level)
 		level = bl->bl_min;
 
 	fd = open ("/sys/devices/platform/omapfb/panel/backlight_level", O_WRONLY);
+	if (fd < 0)
+		return;
+
 	l = snprintf (buffer, NUM_BUF_LEN - 1, "%d", level);
 
-	if (l >= (NUM_BUF_LEN - 1)) {
-		close (fd);
-		return;
-	}
+	if (l < (NUM_BUF_LEN - 1))
+		write (fd, buffer, l);
 
-	write (fd, buffer, l);
 	close (fd);
 }
 
commit b9826360e9149c34e3871cd848168fd7d06b80f3
Author: Guillem Jover <guillem.jover at nokia.com>
Date:   Tue Nov 20 14:37:38 2007 +0100

    fixed indentation
    
    Fixed indentation in hald-addon-omap-backlight.

diff --git a/hald/linux/addons/addon-omap-backlight.c b/hald/linux/addons/addon-omap-backlight.c
index 97cf7a2..b05ccbb 100644
--- a/hald/linux/addons/addon-omap-backlight.c
+++ b/hald/linux/addons/addon-omap-backlight.c
@@ -49,7 +49,7 @@
 
 static GMainLoop *main_loop;
 static LibHalContext *halctx = NULL;
-static char * udi;
+static char *udi;
 static DBusConnection *conn;
 
 #define NUM_BUF_LEN 11
@@ -58,25 +58,26 @@ static char buffer[NUM_BUF_LEN];
 
 struct backlight
 {
-    void (*set_backlight_level)(struct backlight * bl, int i);
-    int (*get_backlight_level)(struct backlight * bl);
-    void (*backlight_init)(struct backlight * bl);
-    int bl_min;
-    int bl_max;
+	void (*set_backlight_level) (struct backlight *bl, int i);
+	int (*get_backlight_level) (struct backlight *bl);
+	void (*backlight_init) (struct backlight *bl);
+	int bl_min;
+	int bl_max;
 };
 
 struct backlight bl_data;
 
 /* Reads backligh level */
-static int read_backlight(struct backlight * bl)
+static int
+read_backlight (struct backlight * bl)
 {
- int fd;
+	int fd;
 
- fd = open("/sys/devices/platform/omapfb/panel/backlight_level", O_RDONLY);
- if(fd <0 || read(fd, buffer, NUM_BUF_LEN) < 0)
-    return -1;
+	fd = open ("/sys/devices/platform/omapfb/panel/backlight_level", O_RDONLY);
+	if (fd < 0 || read (fd, buffer, NUM_BUF_LEN) < 0)
+		return -1;
 
- return atoi(buffer);
+	return atoi (buffer);
 }
 
 /* Read maximum bl level */
@@ -84,97 +85,98 @@ static int read_backlight(struct backlight * bl)
    No we have fixed value in FDI file, but it
    is better to set it in addon code.
 */
-static void backlight_init(struct backlight * bl)
+static void
+backlight_init (struct backlight * bl)
 {
-    int fd;
+	int fd;
 
-    /* Reading maximum backlight level */
-    fd = open("/sys/devices/platform/omapfb/panel/backlight_max", O_RDONLY);
+	/* Reading maximum backlight level */
+	fd = open ("/sys/devices/platform/omapfb/panel/backlight_max", O_RDONLY);
 
-    if(fd <0 || read(fd, buffer, NUM_BUF_LEN - 1) < 0)
-	return;
+	if (fd < 0 || read (fd, buffer, NUM_BUF_LEN - 1) < 0)
+		return;
 
-    bl->bl_max = atoi(buffer);
-    close(fd);
+	bl->bl_max = atoi (buffer);
+	close (fd);
 }
 
 /* Setting backlight level */
-static void write_backlight(struct backlight * bl, int level)
+static void
+write_backlight (struct backlight * bl, int level)
 {
-    int fd, l;
+	int fd, l;
 
-    /* sanity-checking level we're required to set */
-    if(level > bl->bl_max)
-	level = bl->bl_max;
+	/* sanity-checking level we're required to set */
+	if (level > bl->bl_max)
+		level = bl->bl_max;
 
-    if(level < bl->bl_min)
-	level = bl->bl_min;
+	if (level < bl->bl_min)
+		level = bl->bl_min;
 
-    fd = open("/sys/devices/platform/omapfb/panel/backlight_level", O_WRONLY);
-    l = snprintf(buffer, NUM_BUF_LEN - 1, "%d", level);
+	fd = open ("/sys/devices/platform/omapfb/panel/backlight_level", O_WRONLY);
+	l = snprintf (buffer, NUM_BUF_LEN - 1, "%d", level);
 
-    if(l >= (NUM_BUF_LEN - 1)) {
-	close(fd);
-	return;
-    }
+	if (l >= (NUM_BUF_LEN - 1)) {
+		close (fd);
+		return;
+	}
 
-    write(fd, buffer, l);
-    close(fd);
+	write (fd, buffer, l);
+	close (fd);
 }
 
 static gboolean
 check_priv (DBusConnection *connection, DBusMessage *message, const char *udi, const char *privilege)
 #ifdef HAVE_POLKIT
 {
-        gboolean ret;
-        char *polkit_result;
-        const char *invoked_by_syscon_name;
-        DBusMessage *reply;
-        DBusError error;
-
-        ret = FALSE;
-        polkit_result = NULL;
-
-        invoked_by_syscon_name = dbus_message_get_sender (message);
-        
-        dbus_error_init (&error);
-        polkit_result = libhal_device_is_caller_privileged (halctx,
-                                                            udi,
-                                                            privilege,
-                                                            invoked_by_syscon_name,
-                                                            &error);
-        if (polkit_result == NULL) {
-                reply = dbus_message_new_error_printf (message,
-                                                       "org.freedesktop.Hal.Device.Error",
-                                                       "Cannot determine if caller is privileged",
-                                                       privilege, polkit_result);
-                dbus_connection_send (connection, reply, NULL);
-                goto out;
-        }
-        if (strcmp (polkit_result, "yes") != 0) {
-
-                reply = dbus_message_new_error_printf (message,
-                                                       "org.freedesktop.Hal.Device.PermissionDeniedByPolicy",
-                                                       "%s %s <-- (privilege, result)",
-                                                       privilege, polkit_result);
-                dbus_connection_send (connection, reply, NULL);
-                goto out;
-        }
-
-        ret = TRUE;
+	gboolean ret;
+	char *polkit_result;
+	const char *invoked_by_syscon_name;
+	DBusMessage *reply;
+	DBusError error;
+
+	ret = FALSE;
+	polkit_result = NULL;
+
+	invoked_by_syscon_name = dbus_message_get_sender (message);
+
+	dbus_error_init (&error);
+	polkit_result = libhal_device_is_caller_privileged (halctx,
+	                                                    udi,
+	                                                    privilege,
+	                                                    invoked_by_syscon_name,
+	                                                    &error);
+	if (polkit_result == NULL) {
+		reply = dbus_message_new_error_printf (message,
+		                                       "org.freedesktop.Hal.Device.Error",
+		                                       "Cannot determine if caller is privileged",
+		                                       privilege, polkit_result);
+		dbus_connection_send (connection, reply, NULL);
+		goto out;
+	}
+
+	if (strcmp (polkit_result, "yes") != 0) {
+		reply = dbus_message_new_error_printf (message,
+		                                       "org.freedesktop.Hal.Device.PermissionDeniedByPolicy",
+		                                       "%s %s <-- (privilege, result)",
+		                                       privilege, polkit_result);
+		dbus_connection_send (connection, reply, NULL);
+		goto out;
+	}
+
+	ret = TRUE;
 
 out:
-        if (polkit_result != NULL)
-                libhal_free_string (polkit_result);
-        return ret;
+	if (polkit_result != NULL)
+		libhal_free_string (polkit_result);
+	return ret;
 }
 #else
 {
-        return TRUE;
+	return TRUE;
 }
 #endif
 
-
 /* DBus filter function */
 static DBusHandlerResult
 filter_function (DBusConnection *connection, DBusMessage *message, void *userdata)
@@ -182,27 +184,28 @@ filter_function (DBusConnection *connection, DBusMessage *message, void *userdat
 	DBusError err;
 	DBusMessage *reply;
 
-        if (!check_priv (connection, message, dbus_message_get_path (message), "org.freedesktop.hal.power-management.lcd-panel")) {
-                return DBUS_HANDLER_RESULT_HANDLED;
-        }
+	if (!check_priv (connection, message, dbus_message_get_path (message),
+	                 "org.freedesktop.hal.power-management.lcd-panel")) {
+		return DBUS_HANDLER_RESULT_HANDLED;
+	}
 
 #ifdef DEBUG_OMAP_BL
-	dbg ("filter_function: sender=%s destination=%s obj_path=%s interface=%s method=%s", 
-	     dbus_message_get_sender (message), 
-	     dbus_message_get_destination (message), 
-	     dbus_message_get_path (message), 
+	dbg ("filter_function: sender=%s destination=%s obj_path=%s interface=%s method=%s",
+	     dbus_message_get_sender (message),
+	     dbus_message_get_destination (message),
+	     dbus_message_get_path (message),
 	     dbus_message_get_interface (message),
 	     dbus_message_get_member (message));
 #endif
 	reply = NULL;
 
-	if (dbus_message_is_method_call (message, 
-					 "org.freedesktop.Hal.Device.LaptopPanel", 
+	if (dbus_message_is_method_call (message,
+					 "org.freedesktop.Hal.Device.LaptopPanel",
 					 "SetBrightness")) {
 		int brightness;
 
 		dbus_error_init (&err);
-		if (dbus_message_get_args (message, 
+		if (dbus_message_get_args (message,
 					   &err,
 					   DBUS_TYPE_INT32, &brightness,
 					   DBUS_TYPE_INVALID)) {
@@ -210,7 +213,6 @@ filter_function (DBusConnection *connection, DBusMessage *message, void *userdat
 				reply = dbus_message_new_error (message,
 								"org.freedesktop.Hal.Device.LaptopPanel.Invalid",
 								"Brightness has to be between 0 and 228!");
-
 			} else {
 				int return_code;
 
@@ -228,14 +230,13 @@ filter_function (DBusConnection *connection, DBusMessage *message, void *userdat
 
 			dbus_connection_send (connection, reply, NULL);
 		}
-		
-	} else if (dbus_message_is_method_call (message, 
-						"org.freedesktop.Hal.Device.LaptopPanel", 
+	} else if (dbus_message_is_method_call (message,
+						"org.freedesktop.Hal.Device.LaptopPanel",
 						"GetBrightness")) {
 		int brightness;
 
 		dbus_error_init (&err);
-		if (dbus_message_get_args (message, 
+		if (dbus_message_get_args (message,
 					   &err,
 					   DBUS_TYPE_INVALID)) {
 
@@ -256,9 +257,8 @@ filter_function (DBusConnection *connection, DBusMessage *message, void *userdat
 						  DBUS_TYPE_INVALID);
 			dbus_connection_send (connection, reply, NULL);
 		}
-		
 	}
-	
+
 error:
 	if (reply != NULL)
 		dbus_message_unref (reply);
@@ -267,12 +267,13 @@ error:
 }
 
 /* Setting-up backlight structure */
-static void setup_cb(void)
+static void
+setup_cb (void)
 {
- memset(&bl_data, 0, sizeof(struct backlight));
- bl_data.backlight_init = backlight_init;
- bl_data.get_backlight_level = read_backlight;
- bl_data.set_backlight_level = write_backlight;
+	memset (&bl_data, 0, sizeof (struct backlight));
+	bl_data.backlight_init = backlight_init;
+	bl_data.get_backlight_level = read_backlight;
+	bl_data.set_backlight_level = write_backlight;
 }
 
 int
@@ -281,7 +282,7 @@ main (int argc, char *argv[])
 	DBusError err;
 
 	setup_logger ();
-	setup_cb();
+	setup_cb ();
 	udi = getenv ("UDI");
 
 	HAL_DEBUG (("udi=%s", udi));
@@ -296,16 +297,14 @@ main (int argc, char *argv[])
 		return -3;
 	}
 
-
-
 	conn = libhal_ctx_get_dbus_connection (halctx);
 	dbus_connection_setup_with_g_main (conn, NULL);
 
 	dbus_connection_add_filter (conn, filter_function, NULL, NULL);
 
-	if (!libhal_device_claim_interface (halctx, 
-					    "/org/freedesktop/Hal/devices/omapfb_bl", 
-					    "org.freedesktop.Hal.Device.LaptopPanel", 
+	if (!libhal_device_claim_interface (halctx,
+					    "/org/freedesktop/Hal/devices/omapfb_bl",
+					    "org.freedesktop.Hal.Device.LaptopPanel",
 					    "    <method name=\"SetBrightness\">\n"
 					    "      <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n"
 					    "      <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
@@ -317,11 +316,13 @@ main (int argc, char *argv[])
 		HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LaptopPanel'"));
 		return -4;
 	}
+
 	dbus_error_init (&err);
 	if (!libhal_device_addon_is_ready (halctx, udi, &err)) {
 		return -4;
 	}
-	bl_data.backlight_init(&bl_data);
+
+	bl_data.backlight_init (&bl_data);
 	main_loop = g_main_loop_new (NULL, FALSE);
 	g_main_loop_run (main_loop);
 	return 0;


More information about the hal-commit mailing list