Gmane
From: Patrick Ammann <pammann <at> gmx.net>
Subject: Patch: add safer way to get OSyncXMLFormat* from OSyncData*
Newsgroups: gmane.comp.misc.opensync.devel
Date: 2008-08-17 12:00:00 GMT (46 weeks, 4 hours and 48 minutes ago)
Some plugins do use the function "void * osync_data_get_data_ptr(...)" 
without checking
if the objformat is correct, this will lead to a wrong cast and will 
crash sooner or later.

For the internal format: xmlformat (OSyncXMLFormat) i have added a 
better function,
which checks the type before casting.
Maybe for the other known formats we could do the same and drop the 
dangerous
"void * osync_data_get_data_ptr(...)" completely?

Thanks,
Patrick

Index: plugin/opensync_plugin_env.c
===================================================================
--- plugin/opensync_plugin_env.c	(revision 3543)
+++ plugin/opensync_plugin_env.c	(working copy)
@@ -129,7 +129,7 @@
 	//Load all available shared libraries (plugins)
 	if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
 		if (must_exist) {
-			osync_error_set(error, OSYNC_ERROR_GENERIC, "Path is not loadable");
+			osync_error_set(error, OSYNC_ERROR_GENERIC, "Path is not loadable: %s", path);
 			goto error;
 		} else {
 			osync_trace(TRACE_EXIT, "%s: Directory %s does not exist (non-fatal)", __func__, path);
Index: data/opensync_data.c
===================================================================
--- data/opensync_data.c	(revision 3543)
+++ data/opensync_data.c	(working copy)
@@ -176,6 +176,8 @@
 }

 /*! @brief Get a pointer to the data from a data object
+ *
+ * ATTENTION: Make sure to check the objformat before you cast!
  * 
  * @param data The data object
  * @returns a pointer to the data. Do not free this.
@@ -187,6 +189,23 @@
 	return data->data;
 }

+/*! @brief Get a OSyncXMLFormat pointer to the data from a data object
+ * 
+ * @param data The data object
+ * @returns a pointer to the OSyncXMLFormat data. Do not free this.
+ * 
+ */
+OSyncXMLFormat *osync_data_get_data_ptr_xmlformat(OSyncData *data, OSyncError **error)
+{
+	osync_assert(data);
+	const char* formatName= osync_objformat_get_name(data->objformat);
+	if (strncmp(formatName, "xmlformat-", 10) != 0) {
+		osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, "Wrong format: %s", formatName);
+		return NULL;
+	}
+	return (OSyncXMLFormat *)data->data;
+}
+
 /*! @brief Get the data from a data object and then clear the data object's pointers to it
  * 
  * @param data The data object
Index: data/opensync_data.h
===================================================================
--- data/opensync_data.h	(revision 3543)
+++ data/opensync_data.h	(working copy)
@@ -31,7 +31,10 @@
 OSYNC_EXPORT void osync_data_set_objtype(OSyncData *data, const char *objtype);

 OSYNC_EXPORT void osync_data_get_data(OSyncData *data, char **buffer, unsigned int *size);
+
 OSYNC_EXPORT void *osync_data_get_data_ptr(OSyncData *data);
+OSYNC_EXPORT OSyncXMLFormat *osync_data_get_data_ptr_xmlformat(OSyncData *data, OSyncError **error);
+
 OSYNC_EXPORT void osync_data_steal_data(OSyncData *data, char **buffer, unsigned int *size);
 OSYNC_EXPORT void osync_data_set_data(OSyncData *data, char *buffer, unsigned int size);
 OSYNC_EXPORT osync_bool osync_data_has_data(OSyncData *data);
Index: format/opensync_format_env.c
===================================================================
--- format/opensync_format_env.c	(revision 3543)
+++ format/opensync_format_env.c	(working copy)
@@ -53,7 +53,7 @@
 	//Load all available shared libraries (plugins)
 	if (!g_file_test(path, G_FILE_TEST_IS_DIR)) {
 		if (must_exist) {
-			osync_error_set(error, OSYNC_ERROR_GENERIC, "Path is not loadable");
+			osync_error_set(error, OSYNC_ERROR_GENERIC, "Path is not loadable: %s", path);
 			goto error;
 		} else {
 			osync_trace(TRACE_EXIT, "%s: Directory does not exist (non-fatal)", __func__);
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Opensync-devel mailing list
Opensync-devel <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensync-devel