Przeglądaj źródła

confdb.h: error_text vs. buflen

* lib/confdb.c (MIN): Define.
(confdb_write): Use new errbuf_len parameter.
Also note bugs (Chrissie confirms) that error_text is not
set in two error-return cases.
* test/testconfdb.c (do_write_tests): Update use of confdb_write.

git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@2002 fd59a12c-fef9-0310-b244-a6a79926bd2f
Jim Meyering 17 lat temu
rodzic
commit
1308223dab
3 zmienionych plików z 17 dodań i 8 usunięć
  1. 2 1
      include/corosync/confdb.h
  2. 13 5
      lib/confdb.c
  3. 2 2
      test/testconfdb.c

+ 2 - 1
include/corosync/confdb.h

@@ -109,7 +109,8 @@ cs_error_t confdb_finalize (
  */
 cs_error_t confdb_write (
 	confdb_handle_t handle,
-	char *error_text);
+	char *error_text,
+	size_t errbuf_len);
 
 /*
  * Reload the configuration

+ 13 - 5
lib/confdb.c

@@ -53,6 +53,9 @@
 
 #include "sa-confdb.h"
 
+#undef MIN
+#define MIN(x,y) ((x) < (y) ? (x) : (y))
+
 /* Hold the information for iterators so that
    callers can do recursive tree traversals.
    each object_handle can have its own iterator */
@@ -1397,7 +1400,8 @@ error_exit:
 
 cs_error_t confdb_write (
 	confdb_handle_t handle,
-	char *error_text)
+	char *error_text,
+	size_t errbuf_len)
 {
 	cs_error_t error;
 	struct confdb_inst *confdb_inst;
@@ -1407,13 +1411,14 @@ cs_error_t confdb_write (
 
 	error = saHandleInstanceGet (&confdb_handle_t_db, handle, (void *)&confdb_inst);
 	if (error != CS_OK) {
+		/* FIXME: set error_text */
 		return (error);
 	}
 
 	if (confdb_inst->standalone) {
 		error = CS_OK;
 
-		if (confdb_sa_write(error_text))
+		if (confdb_sa_write(error_text, errbuf_len))
 			error = CS_ERR_ACCESS;
 		goto error_exit;
 	}
@@ -1435,12 +1440,16 @@ cs_error_t confdb_write (
 
 	pthread_mutex_unlock (&confdb_inst->response_mutex);
 	if (error != CS_OK) {
+		/* FIXME: set error_text */
 		goto error_exit;
 	}
 
 	error = res_lib_confdb_write.header.error;
-	if (res_lib_confdb_write.error.length)
-		memcpy(error_text, res_lib_confdb_write.error.value, res_lib_confdb_write.error.length);
+	if (res_lib_confdb_write.error.length) {
+		memcpy(error_text, res_lib_confdb_write.error.value,
+		       MIN(res_lib_confdb_write.error.length,errbuf_len));
+		error_text[errbuf_len-1] = '\0';
+	}
 
 error_exit:
 	(void)saHandleInstancePut (&confdb_handle_t_db, handle);
@@ -1600,4 +1609,3 @@ error_exit:
 
 	return (error);
 }
-

+ 2 - 2
test/testconfdb.c

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008 Red Hat Inc
+ * Copyright (c) 2008, 2009 Red Hat Inc
  *
  * All rights reserved.
  *
@@ -188,7 +188,7 @@ static void do_write_tests(confdb_handle_t handle)
 		return;
 	}
 
-	res = confdb_write(handle, error_string);
+	res = confdb_write(handle, error_string, sizeof error_string);
 	printf("confdb_write returned %d: %s\n", res, error_string);
 }