From c3b1e7acf865cb826eacc5fa8b9e2d9b1bddc647 Mon Sep 17 00:00:00 2001
From: alex <alex@infiniteadaptability.org>
Date: Tue, 19 Apr 2022 18:39:23 -0700
Subject: [PATCH] ...

---
 src/opt/watch.c           | 18 +++++++++++-------
 src/torrent/init.c        |  8 +++++++-
 test/unit/torrent.tests.c | 16 ++++++++++++++++
 3 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/opt/watch.c b/src/opt/watch.c
index fe2c056..689fc06 100644
--- a/src/opt/watch.c
+++ b/src/opt/watch.c
@@ -2,7 +2,7 @@
 
 int opt_add_watch(const char *directory) {
 	struct torrent *p;
-	char *name;
+	char *dup, *name;
 
 	if(NULL==directory) { return -1; }
 
@@ -11,18 +11,22 @@ int opt_add_watch(const char *directory) {
 		return -1;
 	}
 
-	name = strdup(directory);
-	if(NULL==name) { return -1; }
-	name = basename(name);
-
 	if(torrent_init(&p,global_options.piece_length)<0) { return -1; }
-
+	
 	p->root = strdup(directory);
 	if(NULL==p->root) { goto clean; }
 
+	/*
+	 * basename modifies buffer in place
+	 * and should not be passed to free(3)
+	 */
+	dup = strdup(directory);
+	if(NULL==dup) { goto clean; }
+	name = basename(dup);
+
 	p->name = strdup(name);
+	free(dup);
 	if(NULL==p->name) { goto clean; }
-	free(name);
 
 	p->feed_url = strdup(global_options.feed_url);
 	if(NULL==p->feed_url) { goto clean; }
diff --git a/src/torrent/init.c b/src/torrent/init.c
index 443cf14..947a252 100644
--- a/src/torrent/init.c
+++ b/src/torrent/init.c
@@ -16,9 +16,10 @@ int torrent_init(struct torrent **torrent_p, unsigned long long piece_length) {
 	(*torrent_p)->root = NULL;
 	(*torrent_p)->name = NULL;
 	(*torrent_p)->feed_url = NULL;
-	(*torrent_p)->pieces = NULL;
+
 	(*torrent_p)->piece_length = piece_length;
 	
+	
 	memset((*torrent_p)->infohash,0,crypto_hash_sha256_BYTES);
 
 	if(tree_init(&((*torrent_p)->tree))<0) { goto clean; }
@@ -26,6 +27,11 @@ int torrent_init(struct torrent **torrent_p, unsigned long long piece_length) {
 	if(hashmap_init(&((*torrent_p)->files.paths),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
 	if(hashmap_init(&((*torrent_p)->files.roots),TORRENT_FILES_HASHMAP_INITIAL_SIZE)<0) { goto clean; }
 
+	(*torrent_p)->pieces = NULL;
+	(*torrent_p)->pieces_size = 0;
+
+	(*torrent_p)->watch_fd = -1;
+
 	return 1;
 	clean:
 		torrent_free(*torrent_p);
diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c
index 211276b..8b5c7e3 100644
--- a/test/unit/torrent.tests.c
+++ b/test/unit/torrent.tests.c
@@ -236,6 +236,22 @@ static void torrent_init_basic_test() {
 	assert(torrent_init(&torrent,1000)<0);
 
 	assert(torrent_init(&torrent,16384)==1);
+
+	assert(NULL==torrent->root);
+	assert(NULL==torrent->name);
+	assert(NULL==torrent->feed_url);
+
+	assert(16384==torrent->piece_length);
+
+	assert(torrent->tree!=NULL);
+	assert(torrent->files.paths!=NULL);
+	assert(torrent->files.roots!=NULL);
+
+	assert(NULL==torrent->pieces);
+	assert(0==torrent->pieces_size);
+
+	assert(-1==torrent->watch_fd);
+
 	torrent_free(torrent);
 }
 
-- 
2.39.5