Skip to content

Commit

Permalink
workaround for string overflow false alarm
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Jul 25, 2020
1 parent 09a4ec9 commit 28c4e30
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/xxh.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ int is_hashable(SEXP key) {

// much of the following is derived from the fastdigest package but adapt to xxh

static char* buf1;

static void OutChar(R_outpstream_t stream, int c) {
XXH3_state_t* const xxh_state = (XXH3_state_t* const) stream->data;
char x = (char) c;
XXH3_64bits_update(xxh_state, &x, 1);
buf1[0] = (char) c;
XXH3_64bits_update(xxh_state, buf1, 1);
}

static void OutBytes(R_outpstream_t stream, void *buf, int length) {
Expand All @@ -55,18 +57,20 @@ static void OutBytes(R_outpstream_t stream, void *buf, int length) {

XXH64_hash_t xxh_serialized_digest(SEXP x) {
XXH3_state_t* const xxh_state = XXH3_createState();
XXH3_64bits_reset_withSeed(xxh_state, 0);
XXH3_64bits_reset(xxh_state);
struct R_outpstream_st stream;
R_pstream_format_t type = R_pstream_binary_format;
int version = 0;

buf1 = malloc(1);
R_InitOutPStream(&stream, (R_pstream_data_t) xxh_state, type, version,
OutChar, OutBytes, NULL, R_NilValue);

R_Serialize(x, &stream);

XXH64_hash_t res = XXH3_64bits_digest(xxh_state);
XXH3_freeState(xxh_state);
free(buf1);
return res;
}

Expand All @@ -80,7 +84,7 @@ XXH64_hash_t xxh_digest(SEXP x) {
return XXH3_64bits(p, strlen(p));
} else {
XXH3_state_t* const xxh_state = XXH3_createState();
XXH3_64bits_reset_withSeed(xxh_state, 0);
XXH3_64bits_reset(xxh_state);
R_xlen_t n = Rf_length(x);
for (R_xlen_t i = 0; i < n; i++) {
p = (char *) Rf_translateCharUTF8(STRING_ELT(x, i));
Expand Down

0 comments on commit 28c4e30

Please sign in to comment.