Skip to content

UBSAN "index out of bounds" for generated C++ code #2073

Closed
@jeroen

Description

UBSAN shows index out of bounds errors for generated C++ code when setting the repeated fields. Is this a bug or am I using it incorrectly? An example test.proto:

syntax = "proto2";
package mytest;

message MYTEST {
  repeated double realValue = 2;
  repeated sint32 intValue = 3;
}

Compile to C++ using protoc test.proto --cpp_out=. Then main.cc looks like this:

#include "test.pb.h"

int main(){
  mytest::MYTEST x;
  x.add_realvalue(123.123);
  x.add_intvalue(123L);
  return 0;
}

Compile everything with sanitizer flags:

g++ -fsanitize=address,undefined,bounds-strict main.cc test.pb.cc \
  $(pkg-config --cflags --libs protobuf)

And then when I run it:

root@8e50c80421c4:~/test# ./a.out
/usr/include/google/protobuf/repeated_field.h:1289:35: runtime error: index 4 out of bounds for type 'double [1]'
/usr/include/google/protobuf/repeated_field.h:1289:35: runtime error: index 4 out of bounds for type 'int [1]'
/usr/include/google/protobuf/repeated_field.h:282:38: runtime error: index 4 out of bounds for type 'int [1]'
/usr/include/google/protobuf/repeated_field.h:282:38: runtime error: index 4 out of bounds for type 'double [1]'

I am using stock gcc and protobuf from Debian Testing:

root@8e50c80421c4:~/test# g++ --version
g++ (Debian 6.1.1-11) 6.1.1 20160802
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@8e50c80421c4:~/test# protoc --version
libprotoc 3.0.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions