From 568dcc7b48238a0df22707dadb93f0f5b62c06b7 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 22 Jan 2017 14:06:35 +0100 Subject: [PATCH] make GCPP_2s also return static string --- src/cadet/gnunet-service-cadet-new_paths.c | 33 ++++++++++++++-------- src/cadet/gnunet-service-cadet-new_paths.h | 4 +-- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/cadet/gnunet-service-cadet-new_paths.c b/src/cadet/gnunet-service-cadet-new_paths.c index aff46db07..bbe9af8b4 100644 --- a/src/cadet/gnunet-service-cadet-new_paths.c +++ b/src/cadet/gnunet-service-cadet-new_paths.c @@ -627,6 +627,7 @@ struct CadetPeer * GCPP_get_peer_at_offset (struct CadetPeerPath *path, unsigned int off) { + GNUNET_assert (off < path->entries_length); return path->entries[off].peer; } @@ -637,26 +638,34 @@ GCPP_get_peer_at_offset (struct CadetPeerPath *path, * @param path path to convert * @return string, to be freed by caller (unlike other *_2s APIs!) */ -char * +const char * GCPP_2s (struct CadetPeerPath *path) { - char *s; - char *old; + static char buf[2048]; + size_t off; + const unsigned int max_plen = sizeof(buf) / 5 - 2; /* 5 characters per entry */ - old = GNUNET_strdup (""); + off = 0; for (unsigned int i = 0; i < path->entries_length; i++) { - GNUNET_asprintf (&s, - "%s %s", - old, - GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path, - i)))); - GNUNET_free_non_null (old); - old = s; + if ( (path->entries_length > max_plen) && + (i == max_plen / 2) ) + off += GNUNET_snprintf (&buf[off], + sizeof (buf) - off, + "... "); + if ( (path->entries_length > max_plen) && + (i > max_plen / 2) && + (i < path->entries_length - max_plen / 2) ) + continue; + off += GNUNET_snprintf (&buf[off], + sizeof (buf) - off, + "%s ", + GNUNET_i2s (GCP_get_id (GCPP_get_peer_at_offset (path, + i)))); } - return old; + return buf; } diff --git a/src/cadet/gnunet-service-cadet-new_paths.h b/src/cadet/gnunet-service-cadet-new_paths.h index 5714368c7..7310d75e6 100644 --- a/src/cadet/gnunet-service-cadet-new_paths.h +++ b/src/cadet/gnunet-service-cadet-new_paths.h @@ -173,9 +173,9 @@ GCPP_get_peer_at_offset (struct CadetPeerPath *path, * Convert a path to a human-readable string. * * @param path path to convert - * @return string, to be freed by caller (unlike other *_2s APIs!) + * @return string, statically allocated */ -char * +const char * GCPP_2s (struct CadetPeerPath *p); -- 2.25.1