Don't fail silently in VersionInfo(), print useful message

Having version.hpp throw an exception when built against a malformed
version number (due to, eg, not having the git tag available in an
automated package build from source bundle) results in _init_ failing
during an otherwise apparently successful dlopen(), as well as the
netgen-mesher executable crashing before hitting main().

This patch both forces an abort and prints a helpful message alerting
the builder/user that something went wrong instead of handing over
either a coredump or a weirdly malfunctioning library with no apparent
cause.  Hopefully this will avoid a repeat of automated Fedora builds
shipping nonfunctional Netgen RPMs for several years!
This commit is contained in:
Monty Montgomery 2022-05-14 23:37:22 -04:00
parent e8ec2b3550
commit e9eae568ff

View File

@ -1,9 +1,10 @@
#ifndef NETGEN_CORE_VERSION_HPP #ifndef NETGEN_CORE_VERSION_HPP
#define NETGEN_CORE_VERSION_HPP #define NETGEN_CORE_VERSION_HPP
#include <ostream> #include <iostream>
#include <string> #include <string>
#include <tuple> #include <tuple>
#include "exception.hpp"
#include "ngcore_api.hpp" #include "ngcore_api.hpp"
@ -18,37 +19,44 @@ namespace ngcore
VersionInfo() = default; VersionInfo() = default;
VersionInfo(std::string vstring) VersionInfo(std::string vstring)
{ {
minor_ = release = patch = 0; std::string save = vstring;
git_hash = ""; try {
if(vstring.substr(0,1) == "v") minor_ = release = patch = 0;
vstring = vstring.substr(1,vstring.size()-1); git_hash = "";
auto dot = vstring.find('.'); if(vstring.substr(0,1) == "v")
mayor_ = std::stoi(vstring.substr(0,dot)); vstring = vstring.substr(1,vstring.size()-1);
if(dot == size_t(-1)) vstring = ""; auto dot = vstring.find('.');
else vstring = vstring.substr(dot+1, vstring.size()-dot-1); mayor_ = std::stoi(vstring.substr(0,dot));
if(!vstring.empty()) if(dot == size_t(-1)) vstring = "";
{ else vstring = vstring.substr(dot+1, vstring.size()-dot-1);
dot = vstring.find('.'); if(!vstring.empty())
minor_ = std::stoi(vstring.substr(0,dot)); {
if (dot == size_t(-1)) vstring = ""; dot = vstring.find('.');
else vstring = vstring.substr(dot+1, vstring.size()-dot-1); minor_ = std::stoi(vstring.substr(0,dot));
if(!vstring.empty()) if (dot == size_t(-1)) vstring = "";
{ else vstring = vstring.substr(dot+1, vstring.size()-dot-1);
dot = vstring.find('-'); if(!vstring.empty())
release = std::stoi(vstring.substr(0,dot)); {
if(dot == size_t(-1)) vstring = ""; dot = vstring.find('-');
else vstring = vstring.substr(dot+1,vstring.size()-dot-1); release = std::stoi(vstring.substr(0,dot));
if(!vstring.empty()) if(dot == size_t(-1)) vstring = "";
{ else vstring = vstring.substr(dot+1,vstring.size()-dot-1);
dot = vstring.find('-'); if(!vstring.empty())
patch = std::stoi(vstring.substr(0,dot)); {
if(dot == size_t(-1)) vstring = ""; dot = vstring.find('-');
else vstring = vstring.substr(dot+1, vstring.size()-dot-1); patch = std::stoi(vstring.substr(0,dot));
if(!vstring.empty()) if(dot == size_t(-1)) vstring = "";
git_hash = vstring; else vstring = vstring.substr(dot+1, vstring.size()-dot-1);
} if(!vstring.empty())
} git_hash = vstring;
} }
}
}
} catch(...) {
std::cerr << "Malformed NETGEN_VERSION (" << save <<"\n";
std::cerr << "Micompiled/mispackaged Netgen library\n";
abort();
}
} }
VersionInfo(const char* cstr) : VersionInfo(std::string(cstr)) { } VersionInfo(const char* cstr) : VersionInfo(std::string(cstr)) { }