summaryrefslogtreecommitdiff
path: root/libgphoto2_port
diff options
context:
space:
mode:
authorMarcus Meissner <marcus@jet.franken.de>2016-03-19 11:39:52 +0100
committerMarcus Meissner <marcus@jet.franken.de>2016-03-19 11:40:33 +0100
commit4d4e61c4a6a585be46c81c6dfcb9d71b2bca0776 (patch)
treed1812a4ba7b3b7097a80ca063fd94bacca761496 /libgphoto2_port
parent2fdb0a4669b6268e7e8c6fb7a2784d4e849775f0 (diff)
downloadlibgphoto2-4d4e61c4a6a585be46c81c6dfcb9d71b2bca0776.tar.gz
use a unique logfunc id, not the array size, which will fall
apart once we remove entries in between working fixes https://github.com/gphoto/libgphoto2/issues/45
Diffstat (limited to 'libgphoto2_port')
-rw-r--r--libgphoto2_port/libgphoto2_port/gphoto2-port-log.c21
-rw-r--r--libgphoto2_port/test/test-gp-port.c18
2 files changed, 31 insertions, 8 deletions
diff --git a/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c b/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c
index 62845bc73..cc03bef65 100644
--- a/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c
+++ b/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c
@@ -81,18 +81,19 @@ static unsigned int log_funcs_count = 0;
int
gp_log_add_func (GPLogLevel level, GPLogFunc func, void *data)
{
- C_PARAMS (func);
+ static int logfuncid = 0;
+ C_PARAMS (func);
C_MEM (log_funcs = realloc (log_funcs, sizeof (LogFunc) *
(log_funcs_count + 1)));
log_funcs_count++;
- log_funcs[log_funcs_count - 1].id = log_funcs_count;
+ log_funcs[log_funcs_count - 1].id = ++logfuncid;
log_funcs[log_funcs_count - 1].level = level;
log_funcs[log_funcs_count - 1].func = func;
log_funcs[log_funcs_count - 1].data = data;
- return (log_funcs_count);
+ return logfuncid;
}
@@ -136,12 +137,16 @@ gpi_vsnprintf (const char* format, va_list args)
int
gp_log_remove_func (int id)
{
- C_PARAMS (id > 0 && id <= log_funcs_count);
-
- memmove (log_funcs + id - 1, log_funcs + id, log_funcs_count - id);
- log_funcs_count--;
+ int i;
- return (GP_OK);
+ for (i=0;i<log_funcs_count;i++) {
+ if (log_funcs[i].id == id) {
+ memmove (log_funcs + i - 1, log_funcs + i, log_funcs_count - i);
+ log_funcs_count--;
+ return GP_OK;
+ }
+ }
+ return GP_ERROR_BAD_PARAMETERS;
}
/**
diff --git a/libgphoto2_port/test/test-gp-port.c b/libgphoto2_port/test/test-gp-port.c
index cf2182ac7..304545398 100644
--- a/libgphoto2_port/test/test-gp-port.c
+++ b/libgphoto2_port/test/test-gp-port.c
@@ -46,6 +46,24 @@ main (int argc, char **argv)
GPPortInfo info;
GPLevel level;
unsigned int i;
+ int id1,id2,id3;
+
+ /* test https://github.com/gphoto/libgphoto2/issues/45 */
+ id1 = gp_log_add_func (GP_LOG_DATA, log_func, NULL);
+ id2 = gp_log_add_func (GP_LOG_DATA, log_func, NULL);
+ if (GP_OK != gp_log_remove_func (id1)) {
+ printf ("id1 not found?\n");
+ return 1;
+ }
+ id3 = gp_log_add_func (GP_LOG_DATA, log_func, NULL);
+ if (GP_OK != gp_log_remove_func (id2)) {
+ printf ("id2 not found?\n");
+ return 1;
+ }
+ if (GP_OK != gp_log_remove_func (id3)) {
+ printf ("id3 not found?\n");
+ return 1;
+ }
gp_log_add_func (GP_LOG_DATA, log_func, NULL);