Files
codebase-memory-mcp/internal/cbm/zstd_store.h
Martin Vogel 7814dacf15 fix: size the compress path with size_t and prune the cache from the walk
Two loose ends from the bounds work.

The decompression side already took size_t so a >2 GiB capacity could not wrap
through int, but compression still took int for both the source length and the
destination capacity, and the artifact export cast a size_t database size down
to reach it. A database past 2 GiB would have handed the encoder a negative
length. Both lengths and the bound helper are size_t now, and the function
returns int64_t like its decompressing counterpart.

The discovery walk now prunes the cache directory by absolute path. A custom
CBM_CACHE_DIR may sit inside a repository — tests do it routinely — and walking
into it pulls every other project's graph database into this project's file
list. This is the narrow form of a concern that was briefly implemented as
refusing any root that contained the cache; refusing a whole root was too blunt,
and not walking the cache is what the concern actually asks for.

The test fails without the prune: a .go file planted under the cache is
otherwise discovered, and "cache" is not in the built-in skip list, so the
assertion is not vacuous.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2026-08-06 16:06:43 +02:00

28 lines
1.3 KiB
C

#ifndef CBM_ZSTD_STORE_H
#define CBM_ZSTD_STORE_H
#include <stddef.h>
#include <stdint.h>
// Zstd compression at specified level (1=fast .. 22=best).
// srcLen/dstCap are size_t for the same reason decompression's are: a >2 GiB
// database would otherwise wrap through int and hand the encoder a capacity that
// disagrees with the real buffer. Returns compressed size on success, 0 on error.
int64_t cbm_zstd_compress(const char *src, size_t srcLen, char *dst, size_t dstCap, int level);
// Zstd decompression. srcLen/dstCap are size_t so a >2 GiB destination capacity
// is never truncated (a large DB artifact, or a crafted one, would otherwise
// wrap through int and desync the real buffer size from the decoder capacity).
// Returns decompressed size on success (>0), 0 on error.
int64_t cbm_zstd_decompress(const char *src, size_t srcLen, char *dst, size_t dstCap);
// Declared decompressed content size of a zstd frame (from its header), or 0
// when unknown/unreadable. Lets the caller size the destination from the frame
// itself instead of trusting a separate, attacker-controllable size field.
size_t cbm_zstd_frame_content_size(const char *src, size_t srcLen);
// Maximum compressed size bound for given input size.
size_t cbm_zstd_compress_bound(size_t inputSize);
#endif // CBM_ZSTD_STORE_H