Skip to content

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

@TimWolla

Description

@TimWolla

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions