lvc / abi-compliance-checker

A tool for checking backward API/ABI compatibility of a C/C++ library
https://lvc.github.io/abi-compliance-checker/
GNU Lesser General Public License v2.1
621 stars 76 forks source link

undetected: constructor parameters in different order than c++ 11 initialization list #88

Open Prime541 opened 5 years ago

Prime541 commented 5 years ago

Hello,

This change looks technically both binary and source compatible but it's not behavior compatible and leads to unexpected behavior at runtime. Could it be possible to detect it and raise a warning or something like a behavior-incompatible error?

// file header1.hpp
class MyClass {
public:
    int myAttr1;
    int myAttr2;
    int getMyAttr1() const {return myAttr1;}
    int getMyAttr2() const {return myAttr2;}
};
// file main1.cpp
#include "header1.hpp"
#include <iostream>

int main() {
    MyClass c = {42, 541};
    std::cout << "myAttr1=" << c.getMyAttr1() << std::endl;
    std::cout << "myAttr2=" << c.getMyAttr2() << std::endl;
}
/*
~/TestInitializerToConstructor
$ g++ --version && g++ -std=c++11 ./main1.cpp -o main1.exe && ./main1.exe
g++ (GCC) 5.4.0
Copyright (C) 2015 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.

myAttr1=42
myAttr2=541
*/

Compare if we replace header1.hpp by header2.hpp (main is unchanged apart from the include).

// file header2.hpp
class MyClass {
public:
    MyClass(int attr2, int attr1) : myAttr1(attr1), myAttr2(attr2) {}
    int myAttr1;
    int myAttr2;
    int getMyAttr1() const {return myAttr1;}
    int getMyAttr2() const {return myAttr2;}
};
// file main2.cpp
#include "header2.hpp"
#include <iostream>

int main() {
    MyClass c = {42, 541};
    std::cout << "myAttr1=" << c.getMyAttr1() << std::endl;
    std::cout << "myAttr2=" << c.getMyAttr2() << std::endl;
}
/*
~/TestInitializerToConstructor
$ g++ --version && g++ -std=c++11 ./main2.cpp -o main2.exe && ./main2.exe
g++ (GCC) 5.4.0
Copyright (C) 2015 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.

myAttr1=541
myAttr2=42
*/