smash-transport / smash-hadron-sampler

Sample hadrons from vHLLE hydrodynamics output for SMASH afterburner.
Other
1 stars 7 forks source link

Change path variables to std::string variables #21

Open nilssass opened 4 months ago

nilssass commented 4 months ago

In params.h and params.cpp the freezeout path sSurface and the output directory sSpectraDir are both stored as C style char strings with a fixed number of characters. This may lead to buffer overflow.

It is safer to swich to the more C++ style std::string class for both paths. Then the calls in params.cpp void readRarams(char* filename) need to be adjusted accordingly to something like:

#include "params.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>

namespace params {

std::string sSurface = "unset";
std::string sSpectraDir = "unset";
std::string sVorticity = "unset";
bool weakContribution = false;
bool rescatter = false;
bool shear = false;
bool bulk = false;
// double Temp = 0.0, mu_b = 0.0, mu_q = 0.0, mu_s = 0.0;
int NEVENTS = 0;
double NBINS = 0.0, QMAX = 0.0;
double dx = 0.0, dy = 0.0, deta = 0.0;
double ecrit = 0.0, cs2 = 0.0, ratio_pressure_energydensity = 0.0;
bool is_spin_sampling_on = false;

void readParams(const std::string& filename) {
    std::ifstream fin(filename);
    if (!fin.is_open()) {
        std::cerr << "cannot open parameters file " << filename << std::endl;
        exit(1);
    }

    std::string line;
    while (std::getline(fin, line)) {
        std::istringstream sline(line);
        std::string parName, parValue;
        sline >> parName >> parValue;

        if (parName == "surface") sSurface = parValue;
        else if (parName == "dbeta") sVorticity = parValue;
        else if (parName == "spectra_dir") sSpectraDir = parValue;
        else if (parName == "Nbins") NBINS = std::stoi(parValue);
        else if (parName == "q_max") QMAX = std::stof(parValue);
        else if (parName == "number_of_events") NEVENTS = std::stoi(parValue);
        else if (parName == "rescatter") rescatter = std::stoi(parValue);
        else if (parName == "weakContribution") weakContribution = std::stoi(parValue);
        else if (parName == "shear") shear = std::stoi(parValue);
        else if (parName == "bulk") bulk = std::stoi(parValue);
        else if (parName == "ecrit") ecrit = std::stof(parValue);
        else if (parName == "cs2") cs2 = std::stof(parValue);
        else if (parName == "ratio_pressure_energydensity") ratio_pressure_energydensity = std::stof(parValue);
        else if (parName == "sample_spin") is_spin_sampling_on = std::stoi(parValue);
        else if (parName[0] == '!') std::cout << "CCC " << sline.str() << std::endl;
        else std::cout << "UUU " << sline.str() << std::endl;
    }

    deta = 0.05;
    dx = dy = 0.0;  // TODO!
}