...
authoralex <[email protected]>
Wed, 20 Apr 2022 01:39:23 +0000 (18:39 -0700)
committeralex <[email protected]>
Wed, 20 Apr 2022 01:39:23 +0000 (18:39 -0700)
src/opt/watch.c
src/torrent/init.c
test/unit/torrent.tests.c

index fe2c05609bcba1f8ae583d01b1d8dd6c8f936010..689fc06cae94b2019425130ad9b3712fec955695 100644 (file)
@@ -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; }
index 443cf14c59a603f5b15d55b199e88531b571fcdd..947a252fb9233cfc54bb03e9fcb57a12120b3d83 100644 (file)
@@ -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);
index 211276b5b635153e0c8f9fbfdee09cd8a53939d3..8b5c7e3336a30eb69d5be8c6b77a082d870da809 100644 (file)
@@ -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);
 }