Files
codebase-memory-mcp/internal/cbm/zstd_store.c
Martin Vogel a2f09c7ce9 artifact: size import decompression from the zstd frame header
The import path allocated the destination from the plaintext original_size
field in artifact.json and passed that value to the decoder truncated
through int. A large real database already wraps that cast (a full-kernel
index is ~14 GB), and a doctored field could additionally desync the decoder
capacity from the actual buffer. Read the decompressed size from the zstd
frame's own content-size header, require the metadata field to match it, cap
the total, and run the decoder with a size_t capacity equal to the
allocation. cbm_zstd_decompress now takes size_t lengths and returns int64_t.
Adds a regression test that a mismatched original_size is rejected.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2026-07-07 20:41:06 +02:00

37 lines
978 B
C

// zstd_store.c — Thin C wrappers around Zstandard.
#include "vendored/zstd/zstd.h"
#include "zstd_store.h"
#include <stddef.h>
#include <stdint.h>
int cbm_zstd_compress(const char *src, int srcLen, char *dst, int dstCap, int level) {
size_t rc = ZSTD_compress(dst, (size_t)dstCap, src, (size_t)srcLen, level);
if (ZSTD_isError(rc)) {
return 0;
}
return (int)rc;
}
int64_t cbm_zstd_decompress(const char *src, size_t srcLen, char *dst, size_t dstCap) {
size_t rc = ZSTD_decompress(dst, dstCap, src, srcLen);
if (ZSTD_isError(rc)) {
return 0;
}
return (int64_t)rc;
}
size_t cbm_zstd_frame_content_size(const char *src, size_t srcLen) {
unsigned long long n = ZSTD_getFrameContentSize(src, srcLen);
if (n == ZSTD_CONTENTSIZE_UNKNOWN || n == ZSTD_CONTENTSIZE_ERROR) {
return 0;
}
return (size_t)n;
}
size_t cbm_zstd_compress_bound(int inputSize) {
return ZSTD_compressBound((size_t)inputSize);
}