jessek / hashdeep

Other
702 stars 132 forks source link

hash.cpp: compute_hash not only clearing buffer, but reallocating the buffer array #352

Open wadechandler opened 8 years ago

wadechandler commented 8 years ago

I'm not sure if per the comment plus the call to memset you mean for this to happen, but compute_hash is not only overwriting the buffer with memset, but reallocating it for every iteration of the while loop while reading file chunks. I see this:

    /*
     * We may need to read multiple times; don't read more than
     * file_data_hasher_t::MD5DEEP_IDEAL_BLOCK_SIZE
     * at a time.
     */

    hc1->read_offset = request_start;
    hc1->read_len    = 0;       // so far

    while (request_len>0){
    // Clear the buffer in case we hit an error and need to pad the hash 
    // The use of MD5DEEP_IDEAL_BLOCK_SIZE means that we loop even for memory-mapped
    // files. That's okay, becuase our super-fast SHA1 implementation
    // actually corrupts its input buffer, forcing a copy...

    unsigned char buffer_[file_data_hasher_t::MD5DEEP_IDEAL_BLOCK_SIZE];
    const unsigned char *buffer = buffer_;
    uint64_t toread = min(request_len,file_data_hasher_t::MD5DEEP_IDEAL_BLOCK_SIZE); // and shrink

    if(this->base==0){      // not mmap, so clear buffer
        memset(buffer_,0,sizeof(buffer_));
    }

and it seems it should be this:

    /*
     * We may need to read multiple times; don't read more than
     * file_data_hasher_t::MD5DEEP_IDEAL_BLOCK_SIZE
     * at a time.
     */

    hc1->read_offset = request_start;
    hc1->read_len    = 0;       // so far

    unsigned char buffer_[file_data_hasher_t::MD5DEEP_IDEAL_BLOCK_SIZE];
    const unsigned char *buffer = buffer_;

    while (request_len>0){
    // Clear the buffer in case we hit an error and need to pad the hash 
    // The use of MD5DEEP_IDEAL_BLOCK_SIZE means that we loop even for memory-mapped
    // files. That's okay, becuase our super-fast SHA1 implementation
    // actually corrupts its input buffer, forcing a copy...

    uint64_t toread = min(request_len,file_data_hasher_t::MD5DEEP_IDEAL_BLOCK_SIZE); // and shrink

    if(this->base==0){      // not mmap, so clear buffer
        memset(buffer_,0,sizeof(buffer_));
    }