Skip to content

Instantly share code, notes, and snippets.

@velavokr
Created June 21, 2016 17:49
Show Gist options
  • Save velavokr/6dbb7add70a5051184309f9848b50cef to your computer and use it in GitHub Desktop.
Save velavokr/6dbb7add70a5051184309f9848b50cef to your computer and use it in GitHub Desktop.
The minimal code needed to reproduce the ZDICT_trainFromBuffer crash
#include <assert.h>
#include <fcntl.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <zdict.h>
#include <zdict_static.h>
typedef struct my_buffer {
char* data;
size_t size;
} my_buffer_t;
size_t get_size(int fd) {
struct stat s;
memset(&s, 0, sizeof(s));
assert(fstat(fd, &s) != -1);
return s.st_size;
}
my_buffer_t read_buffer(char* fname) {
int fd = open(fname, O_RDONLY);
assert(fd >= 0);
struct my_buffer b;
b.size = get_size(fd);
b.data = malloc(b.size);
assert(b.data);
assert(read(fd, b.data, b.size) == b.size);
close(fd);
return b;
}
int main() {
my_buffer_t samples = read_buffer("ZDICT_trainFromBuffer_samplesBuffer.bin");
my_buffer_t lengths = read_buffer("ZDICT_trainFromBuffer_samplesSizes.64bitLE.bin");
size_t nbSamples = lengths.size / sizeof(size_t);
size_t capacity = samples.size + 8 * lengths.size;
char* buffer = malloc(capacity);
assert(buffer);
printf("%zu %zu %zu %zu\n", samples.size, lengths.size, nbSamples, capacity);
ZDICT_params_t params;
memset(&params, 0, sizeof(params));
params.notificationLevel = 3;
ZDICT_trainFromBuffer_advanced(buffer, capacity, samples.data, (const size_t*)lengths.data, nbSamples, params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment