From 2292e911b9462754290309a457ae9b79e0c3b65c Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 5 Dec 2021 12:33:10 -0800 Subject: [PATCH] ... --- src/bencode/encode.c | 11 +++++++---- test/unit/Makefile.am | 4 +++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/bencode/encode.c b/src/bencode/encode.c index 727e608..536532a 100644 --- a/src/bencode/encode.c +++ b/src/bencode/encode.c @@ -60,12 +60,15 @@ ssize_t bencode_string(const uint8_t *str, size_t len, uint8_t *output, size_t o if(NULL==output) { return -1; } i = size_int(len); - i += 2; // account for ':' and '\0' - i += len; + i++; // account for ':' + if(i+len>output_size) { return -1; } - if(i>output_size) { return -1; } + // snprintf requires space for '\0' otherwise will truncate + if(snprintf((char*)output,i+1,"%lu:",len)!=i) { return -1; } - return snprintf((char*)output,i,"%lu:%s",len,(const char*)str); + memcpy(&(output[i]),str,len); + + return i+len; } diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index c8e471e..62c5b31 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -71,7 +71,9 @@ torrent_tests_SOURCES = \ $(top_srcdir)/src/fs/concat.c \ $(top_srcdir)/src/hash.c \ $(top_srcdir)/src/hashmap.c \ - $(top_srcdir)/src/torrent.c \ + $(top_srcdir)/src/torrent/add.c \ + $(top_srcdir)/src/torrent/init.c \ + $(top_srcdir)/src/torrent/free.c \ $(top_srcdir)/src/tree.c tree_tests_SOURCES = \ -- 2.30.2