mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2025-02-25 15:10:48 +01:00
Store compression overhead from tor_compress_state_t
.
The `tor_compress_state_t` data-type is used as a wrapper around the more specialized state-types used by the various compression backends. This patch ensures that the overhead of this "thin" wrapper type is included in the value returned by `tor_compress_get_total_allocation()`. See: https://bugs.torproject.org/22066
This commit is contained in:
parent
fac8ac0e4a
commit
08d86e8408
1 changed files with 21 additions and 7 deletions
|
@ -27,6 +27,9 @@
|
|||
#include "compress_zlib.h"
|
||||
#include "compress_zstd.h"
|
||||
|
||||
/** Total number of bytes allocated for compression state overhead. */
|
||||
static atomic_counter_t total_compress_allocation;
|
||||
|
||||
/** @{ */
|
||||
/* These macros define the maximum allowable compression factor. Anything of
|
||||
* size greater than CHECK_FOR_COMPRESSION_BOMB_AFTER is not allowed to
|
||||
|
@ -324,7 +327,8 @@ tor_compress_header_version_str(compress_method_t method)
|
|||
size_t
|
||||
tor_compress_get_total_allocation(void)
|
||||
{
|
||||
return tor_zlib_get_total_allocation() +
|
||||
return atomic_counter_get(&total_compress_allocation) +
|
||||
tor_zlib_get_total_allocation() +
|
||||
tor_lzma_get_total_allocation() +
|
||||
tor_zstd_get_total_allocation();
|
||||
}
|
||||
|
@ -389,6 +393,8 @@ tor_compress_new(int compress, compress_method_t method,
|
|||
goto err;
|
||||
}
|
||||
|
||||
atomic_counter_add(&total_compress_allocation,
|
||||
sizeof(tor_compress_state_t));
|
||||
return state;
|
||||
|
||||
err:
|
||||
|
@ -461,6 +467,8 @@ tor_compress_free(tor_compress_state_t *state)
|
|||
break;
|
||||
}
|
||||
|
||||
atomic_counter_sub(&total_compress_allocation,
|
||||
sizeof(tor_compress_state_t));
|
||||
tor_free(state);
|
||||
}
|
||||
|
||||
|
@ -470,27 +478,33 @@ tor_compress_state_size(const tor_compress_state_t *state)
|
|||
{
|
||||
tor_assert(state != NULL);
|
||||
|
||||
size_t size = sizeof(tor_compress_state_t);
|
||||
|
||||
switch (state->method) {
|
||||
case GZIP_METHOD:
|
||||
case ZLIB_METHOD:
|
||||
return tor_zlib_compress_state_size(state->u.zlib_state);
|
||||
size += tor_zlib_compress_state_size(state->u.zlib_state);
|
||||
break;
|
||||
case LZMA_METHOD:
|
||||
return tor_lzma_compress_state_size(state->u.lzma_state);
|
||||
size += tor_lzma_compress_state_size(state->u.lzma_state);
|
||||
break;
|
||||
case ZSTD_METHOD:
|
||||
return tor_zstd_compress_state_size(state->u.zstd_state);
|
||||
size += tor_zstd_compress_state_size(state->u.zstd_state);
|
||||
break;
|
||||
case NO_METHOD:
|
||||
case UNKNOWN_METHOD:
|
||||
goto err;
|
||||
break;
|
||||
}
|
||||
|
||||
err:
|
||||
return 0;
|
||||
return size;
|
||||
}
|
||||
|
||||
/** Initialize all compression modules. */
|
||||
void
|
||||
tor_compress_init(void)
|
||||
{
|
||||
atomic_counter_init(&total_compress_allocation);
|
||||
|
||||
tor_zlib_init();
|
||||
tor_lzma_init();
|
||||
tor_zstd_init();
|
||||
|
|
Loading…
Add table
Reference in a new issue