Skip to content

Commit

Permalink
Combine digest_new and digest_insert. Check for malloc errors inside
Browse files Browse the repository at this point in the history
digest_new, add error checks to callers of digest_insert.

Signed-off-by: Mark Fasheh <mfasheh@suse.de>
  • Loading branch information
Mark Fasheh committed Mar 16, 2015
1 parent 5f4cb6b commit 5c095fe
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
23 changes: 16 additions & 7 deletions d_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>

#include "csum.h"
#include "d_tree.h"
#include "debug.h"

struct d_tree *digest_new(unsigned char *digest)
static struct d_tree *digest_new(unsigned char *digest)
{
struct d_tree *token = malloc(sizeof(struct d_tree));

if (token) {
rb_init_node(&token->t_node);
token->digest = malloc(sizeof(unsigned char) * digest_len);
if (!token->digest) {
free(token);
return NULL;
}
memcpy(token->digest, digest, digest_len);
}
return token;
}

int digest_insert(struct rb_root *root, struct d_tree *token)
int digest_insert(struct rb_root *root, unsigned char *digest)
{
struct d_tree *token;
struct rb_node **p = &root->rb_node;
struct rb_node *parent = NULL;
struct d_tree *tmp;
Expand All @@ -42,20 +48,23 @@ int digest_insert(struct rb_root *root, struct d_tree *token)
while (*p) {
parent = *p;

tmp = rb_entry(parent, struct d_tree, t_node);
tmp = rb_entry(parent, struct d_tree, t_node);

cmp = memcmp(token->digest, tmp->digest, digest_len);
cmp = memcmp(digest, tmp->digest, digest_len);
if (cmp < 0)
p = &(*p)->rb_left;
else if (cmp > 0)
p = &(*p)->rb_right;
else {
free(token->digest);
free(token);
return -1;
/* Already exists in tree, nothing more to do */
return 0;
}
}

token = digest_new(digest);
if (!token)
return ENOMEM;

rb_link_node(&token->t_node, parent, p);
rb_insert_color(&token->t_node, root);
return 0;
Expand Down
6 changes: 3 additions & 3 deletions d_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ struct d_tree {
struct rb_node t_node;
};

struct d_tree *digest_new(unsigned char *digest);
int digest_insert(struct rb_root *root, struct d_tree *token);
/* alloc and insert a new digest into the tree */
int digest_insert(struct rb_root *root, unsigned char *digest);
struct d_tree *digest_find(struct rb_root *root,
unsigned char *digest);
unsigned char *digest);

uint64_t digest_count(struct rb_root *root);

Expand Down
4 changes: 1 addition & 3 deletions file_scan.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,7 @@ static void csum_whole_file_swap(struct filerec *file,
ret = bloom_add(&params->bloom,
hashes[i].digest, DIGEST_LEN_MAX);
if (ret == 1) {
d_tree = digest_new(hashes[i].digest);
digest_insert(tree, d_tree);
ret = 0;
ret = digest_insert(tree, hashes[i].digest);
if (ret)
goto err;
params->bloom_match++;
Expand Down
7 changes: 4 additions & 3 deletions serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ static int read_one_file(int fd, struct hash_tree *tree,
struct block_hash bhash;
struct filerec *file;
char fname[PATH_MAX+1];
struct d_tree *d_tree;

ret = read_file(fd, &finfo, fname);
if (ret)
Expand All @@ -283,8 +282,10 @@ static int read_one_file(int fd, struct hash_tree *tree,
ret = bloom_add(&bloom, (unsigned char *)bhash.digest,
DIGEST_LEN_MAX);
if (ret == 1) {
d_tree = digest_new((unsigned char *)bhash.digest);
digest_insert(scan_tree, d_tree);
ret = digest_insert(scan_tree,
((unsigned char *)bhash.digest));
if (ret)
return ret;
}
continue;
}
Expand Down

0 comments on commit 5c095fe

Please sign in to comment.