commit 3a2ca390d9756f17f1c54e74bbc922cbbc38ccd0
parent 285bfd8ab9c332ebc99a0896e2346b56c2d7355a
Author: Jacki <jacki@thejackimonster.de>
Date: Tue, 4 Feb 2025 04:26:40 +0100
Fix memory leaks in ping tool
Signed-off-by: Jacki <jacki@thejackimonster.de>
Diffstat:
1 file changed, 74 insertions(+), 49 deletions(-)
diff --git a/tools/gnunet_messenger_ping.c b/tools/gnunet_messenger_ping.c
@@ -47,6 +47,7 @@ struct GNUNET_MESSENGER_PingTool
uint count;
int public_room;
int auto_pong;
+ int require_pong;
bool quit;
};
@@ -119,50 +120,26 @@ member_callback (void *cls,
static bool
is_hash_following (struct GNUNET_MESSENGER_PingTool *tool,
const struct GNUNET_HashCode *hash,
- const struct GNUNET_HashCode *prev);
-
-struct GNUNET_MESSENGER_HashCheck
-{
- struct GNUNET_MESSENGER_PingTool *tool;
- const struct GNUNET_HashCode *hash;
- bool result;
-};
-
-static enum GNUNET_GenericReturnValue
-multiple_iterator_callback (void *cls,
- const struct GNUNET_HashCode *hash,
- void *value)
+ const struct GNUNET_HashCode *prev)
{
- struct GNUNET_MESSENGER_HashCheck *check = cls;
- const struct GNUNET_HashCode *prev = value;
+ if (!prev)
+ return false;
- if (is_hash_following (check->tool, prev, check->hash))
+ bool first = true;
+ while (hash)
{
- check->result = true;
- return GNUNET_NO;
- }
-
- return GNUNET_YES;
-}
-
-static bool
-is_hash_following (struct GNUNET_MESSENGER_PingTool *tool,
- const struct GNUNET_HashCode *hash,
- const struct GNUNET_HashCode *prev)
-{
- if (0 == GNUNET_CRYPTO_hash_cmp(hash, prev))
- return true;
+ if (0 == GNUNET_CRYPTO_hash_cmp(hash, prev))
+ return true;
- struct GNUNET_MESSENGER_HashCheck check;
- check.tool = tool;
- check.hash = prev;
- check.result = false;
+ if (first)
+ first = false;
+ else if (0 == GNUNET_CRYPTO_hash_cmp(hash + 1, prev))
+ return true;
- GNUNET_CONTAINER_multihashmap_get_multiple(tool->map, hash,
- multiple_iterator_callback,
- &check);
+ hash = GNUNET_CONTAINER_multihashmap_get(tool->map, hash);
+ }
- return check.result;
+ return false;
}
static void
@@ -321,21 +298,18 @@ message_callback (void *cls,
goto skip_ping;
}
+ if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_contains(tool->map, hash))
{
- struct GNUNET_HashCode *copy = GNUNET_new(struct GNUNET_HashCode);
+ struct GNUNET_HashCode *copy = GNUNET_malloc(sizeof(struct GNUNET_HashCode) * 2);
GNUNET_memcpy(copy, &(message->header.previous), sizeof (*copy));
- GNUNET_CONTAINER_multihashmap_put(tool->map, hash, copy,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
- }
-
- if (GNUNET_MESSENGER_KIND_MERGE == message->header.kind)
- {
- struct GNUNET_HashCode *copy = GNUNET_new(struct GNUNET_HashCode);
- GNUNET_memcpy(copy, &(message->body.merge.previous), sizeof (*copy));
+ if (GNUNET_MESSENGER_KIND_MERGE == message->header.kind)
+ GNUNET_memcpy(copy + 1, &(message->body.merge.previous), sizeof (*copy));
+ else
+ GNUNET_memcpy(copy + 1, &(message->header.previous), sizeof (*copy));
GNUNET_CONTAINER_multihashmap_put(tool->map, hash, copy,
- GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
}
if (GNUNET_MESSENGER_FLAG_SENT & flags)
@@ -390,6 +364,11 @@ message_callback (void *cls,
ping->traffic++;
+ if ((tool->require_pong) &&
+ ((GNUNET_MESSENGER_KIND_TAG != message->header.kind) ||
+ (0 != GNUNET_CRYPTO_hash_cmp(&(message->body.tag.hash), &key))))
+ continue;
+
if (NULL != GNUNET_CONTAINER_multishortmap_get(ping->pong_map, hash_contact (sender)))
continue;
@@ -532,6 +511,43 @@ run (void *cls,
);
}
+enum GNUNET_GenericReturnValue
+free_map_time (void *cls,
+ const struct GNUNET_ShortHashCode *key,
+ void *value)
+{
+ struct GNUNET_TIME_Absolute *time = value;
+
+ if (time)
+ GNUNET_free(time);
+
+ return GNUNET_YES;
+}
+
+enum GNUNET_GenericReturnValue
+free_map_ping (void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
+{
+ struct GNUNET_MESSENGER_Ping *ping = value;
+
+ GNUNET_CONTAINER_multishortmap_iterate(ping->pong_map, free_map_time, NULL);
+ GNUNET_CONTAINER_multishortmap_destroy(ping->pong_map);
+
+ GNUNET_free(ping);
+ return GNUNET_YES;
+}
+
+enum GNUNET_GenericReturnValue
+free_map_hashes (void *cls,
+ const struct GNUNET_HashCode *key,
+ void *value)
+{
+ struct GNUNET_HashCode *hashes = value;
+ GNUNET_free(hashes);
+ return GNUNET_YES;
+}
+
int
main (int argc,
char* const* argv)
@@ -570,9 +586,15 @@ main (int argc,
GNUNET_GETOPT_option_flag(
'P',
"pong",
- "only send back messages after a ping",
+ "only send back pong messages after a ping",
&(tool.auto_pong)
),
+ GNUNET_GETOPT_option_flag(
+ 'R',
+ "require-pong",
+ "only react to pong messages after a ping",
+ &(tool.require_pong)
+ ),
GNUNET_GETOPT_OPTION_END
};
@@ -589,6 +611,9 @@ main (int argc,
&tool
);
+ GNUNET_CONTAINER_multihashmap_iterate(tool.ping_map, free_map_ping, NULL);
+ GNUNET_CONTAINER_multihashmap_iterate(tool.map, free_map_hashes, NULL);
+
GNUNET_CONTAINER_multihashmap_destroy(tool.ping_map);
GNUNET_CONTAINER_multihashmap_destroy(tool.map);