HdrHistogram_c icon indicating copy to clipboard operation
HdrHistogram_c copied to clipboard

undefined behavior (left shift of negative value) when encoding an empty histogram

Open TimWolla opened this issue 1 year ago • 0 comments

Consider the following program:

#include <stdlib.h>
#include "hdr/hdr_histogram.h"
#include "hdr/hdr_histogram_log.h"
#include "hdr/hdr_histogram_version.h"

int main() {
	struct hdr_histogram* histogram;
	char *result = NULL;

	hdr_init(1, 100, 1, &histogram);
	hdr_log_encode(histogram, &result);

	printf("%s\n", result);

	free(result);
	hdr_close(histogram);
}

Building the library with clang's address sanitizer via:

env CFLAGS="-g -O0 -fsanitize=undefined,address" LDFLAGS="-fsanitize=undefined,address" CC=clang CXX=clang++ cmake .

Then Compiling the test program using:

clang test.c -I/usr/local/include  -L/usr/local/lib -lhdr_histogram  -g -fsanitize=undefined,address

And executing ./a.out results in:

HdrHistogram_c/src/hdr_encoding.c:19:20: runtime error: left shift of negative value -1
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior HdrHistogram_c/src/hdr_encoding.c:19:20 in 
HISTFAAAABl4nJNpmSzMwMDAyAAB6HSK/QeoAABAagLe

TimWolla avatar Mar 18 '24 16:03 TimWolla