replaced sprintf -> snprintf throughout.

This commit is contained in:
2022-11-05 20:16:44 +01:00
parent eb63f5862b
commit e32aa61643
27 changed files with 120 additions and 120 deletions

View File

@@ -3863,7 +3863,7 @@ Bool_t PMsrHandler::FilterNumber(TString str, const Char_t *filter, Int_t offset
Char_t *cstr, filterStr[32];
cstr = new Char_t[str.Sizeof()];
strncpy(cstr, str.Data(), str.Sizeof());
sprintf(filterStr, "%s%%d", filter);
snprintf(filterStr, sizeof(filterStr), "%s%%d", filter);
// get number if present
found = sscanf(cstr, filterStr, &no_found);
@@ -6974,7 +6974,7 @@ UInt_t PMsrHandler::LastSignificant(Double_t dval, UInt_t precLimit)
char str[128];
sprintf(str, "%lf", dval);
snprintf(str, sizeof(str), "%lf", dval);
// find decimal point
for (UInt_t i=0; i<strlen(str); i++) {

View File

@@ -545,7 +545,7 @@ void PMusrCanvas::UpdateParamTheoryPad()
for (UInt_t i=0; i<param.size(); i++) {
str = "";
accuracy = GetNeededAccuracy(param[i]);
sprintf(accStr, "%%.%dlf", accuracy);
snprintf(accStr, sizeof(accStr), "%%.%dlf", accuracy);
// parameter no
str += param[i].fNo;
if (param[i].fNo<10)
@@ -558,9 +558,9 @@ void PMusrCanvas::UpdateParamTheoryPad()
str += " ";
// parameter value
if (round(param[i].fValue)-param[i].fValue==0)
sprintf(cnum, "%.1lf", param[i].fValue);
snprintf(cnum, sizeof(cnum), "%.1lf", param[i].fValue);
else
sprintf(cnum, accStr, param[i].fValue);
snprintf(cnum, sizeof(cnum), accStr, param[i].fValue);
str += cnum;
for (Int_t j=0; j<9-(Int_t)strlen(cnum); j++) // fill spaces
str += " ";
@@ -574,22 +574,22 @@ void PMusrCanvas::UpdateParamTheoryPad()
if ((fabs(fabs(param[i].fStep) - param[i].fPosError) < 0.1*fabs(param[i].fStep)) &&
(fabs(fabs(param[i].fStep) - param[i].fPosError) < 0.1*param[i].fPosError)) {
if (round(err)-err==0)
sprintf(cnum, "%.1lf", err);
snprintf(cnum, sizeof(cnum), "%.1lf", err);
else
sprintf(cnum, accStr, err);
snprintf(cnum, sizeof(cnum), accStr, err);
} else {
sprintf(accStr, "%%.%dlf!!", accuracy);
snprintf(accStr, sizeof(accStr), "%%.%dlf!!", accuracy);
if (round(err)-err==0)
sprintf(cnum, "%.1lf!!", err);
snprintf(cnum, sizeof(cnum), "%.1lf!!", err);
else
sprintf(cnum, accStr, err);
snprintf(cnum, sizeof(cnum), accStr, err);
}
str += cnum;
} else { // minos was not used
if (round(param[i].fStep)-param[i].fStep==0)
sprintf(cnum, "%.1lf", param[i].fStep);
snprintf(cnum, sizeof(cnum), "%.1lf", param[i].fStep);
else
sprintf(cnum, accStr, param[i].fStep);
snprintf(cnum, sizeof(cnum), accStr, param[i].fStep);
str += cnum;
}
ypos = 0.98-i*yoffset;
@@ -938,13 +938,13 @@ void PMusrCanvas::UpdateInfoPad()
tstr += TString("??,");
} else if (ddvec->size() == 1){
tstr += TString("T=");
sprintf(sval, "%0.2lf", ddvec->at(0).first);
snprintf(sval, sizeof(sval), "%0.2lf", ddvec->at(0).first);
tstr += TString(sval) + TString("K,");
} else {
for(UInt_t i(0); i<ddvec->size(); ++i){
sprintf(sval, "T%u=", i);
snprintf(sval, sizeof(sval), "T%u=", i);
tstr += TString(sval);
sprintf(sval, "%0.2lf", ddvec->at(i).first);
snprintf(sval, sizeof(sval), "%0.2lf", ddvec->at(i).first);
tstr += TString(sval) + TString("K,");
}
}
@@ -955,10 +955,10 @@ void PMusrCanvas::UpdateInfoPad()
tstr += TString("??,");
} else {
if (dval < 1.0e4) { // Gauss makes sense as a unit
sprintf(sval, "%0.2lf", dval);
snprintf(sval, sizeof(sval), "%0.2lf", dval);
tstr += TString(sval) + TString("G,");
} else { // Tesla makes sense as a unit
sprintf(sval, "%0.2lf", dval/1.0e4);
snprintf(sval, sizeof(sval), "%0.2lf", dval/1.0e4);
tstr += TString(sval) + TString("T,");
}
}
@@ -969,10 +969,10 @@ void PMusrCanvas::UpdateInfoPad()
tstr += TString("??,");
} else {
if (dval < 1.0e3) { // keV makes sense as a unit
sprintf(sval, "%0.2lf", dval);
snprintf(sval, sizeof(sval), "%0.2lf", dval);
tstr += TString(sval) + TString("keV,");
} else { // MeV makes sense as a unit
sprintf(sval, "%0.2lf", dval/1.0e3);
snprintf(sval, sizeof(sval), "%0.2lf", dval/1.0e3);
tstr += TString(sval) + TString("MeV,");
}
}
@@ -1581,13 +1581,13 @@ void PMusrCanvas::SaveGraphicsAndQuit(Char_t *fileName, Char_t *graphicsFormat)
}
if (fStartWithFourier)
sprintf(ext, "_%d_F", fPlotNumber);
snprintf(ext, sizeof(ext), "_%d_F", fPlotNumber);
else
sprintf(ext, "_%d", fPlotNumber);
snprintf(ext, sizeof(ext), "_%d", fPlotNumber);
str.Replace(idx, size, ext, strlen(ext));
idx += strlen(ext);
size = strlen(ext);
sprintf(ext, ".%s", graphicsFormat);
snprintf(ext, sizeof(ext), ".%s", graphicsFormat);
str.Replace(idx, size, ext, strlen(ext));
std::cout << std::endl << ">> SaveGraphicsAndQuit: " << str.Data() << std::endl;
@@ -6546,7 +6546,7 @@ UInt_t PMusrCanvas::GetNeededAccuracy(PMsrParamStructure param)
if (param.fStep == 0.0) { // check if fit parameter is a constant, i.e. step==0
char str[128];
sprintf(str, "%lf", param.fValue);
snprintf(str, sizeof(str), "%lf", param.fValue);
// find decimal point
for (UInt_t i=0; i<strlen(str); i++) {

View File

@@ -823,9 +823,9 @@ Bool_t PRunDataHandler::ReadWriteFilesList()
else // bzip2
fln += TString(".tar.bz2");
if (fAny2ManyInfo->compressionTag == 1) // gzip
sprintf(cmd, "tar -zcf %s %s", fln.Data(), fAny2ManyInfo->outPathFileName[0].Data());
snprintf(cmd, sizeof(cmd), "tar -zcf %s %s", fln.Data(), fAny2ManyInfo->outPathFileName[0].Data());
else // bzip2
sprintf(cmd, "tar -jcf %s %s", fln.Data(), fAny2ManyInfo->outPathFileName[0].Data());
snprintf(cmd, sizeof(cmd), "tar -jcf %s %s", fln.Data(), fAny2ManyInfo->outPathFileName[0].Data());
if (system(cmd) == -1) {
std::cerr << "**ERROR** cmd: " << cmd << " failed." << std::endl;
}
@@ -833,19 +833,19 @@ Bool_t PRunDataHandler::ReadWriteFilesList()
fln += TString(".tar");
for (UInt_t i=0; i<fAny2ManyInfo->outPathFileName.size(); i++) {
if (i==0) {
sprintf(cmd, "tar -cf %s %s", fln.Data(), fAny2ManyInfo->outPathFileName[i].Data());
snprintf(cmd, sizeof(cmd), "tar -cf %s %s", fln.Data(), fAny2ManyInfo->outPathFileName[i].Data());
} else {
sprintf(cmd, "tar -rf %s %s", fln.Data(), fAny2ManyInfo->outPathFileName[i].Data());
snprintf(cmd, sizeof(cmd), "tar -rf %s %s", fln.Data(), fAny2ManyInfo->outPathFileName[i].Data());
}
if (system(cmd) == -1) {
std::cerr << "**ERROR** cmd: " << cmd << " failed." << std::endl;
}
}
if (fAny2ManyInfo->compressionTag == 1) { // gzip
sprintf(cmd, "gzip %s", fln.Data());
snprintf(cmd, sizeof(cmd), "gzip %s", fln.Data());
fln += ".gz";
} else {
sprintf(cmd, "bzip2 -z %s", fln.Data());
snprintf(cmd, sizeof(cmd), "bzip2 -z %s", fln.Data());
fln += ".bz2";
}
if (system(cmd) == -1) {
@@ -1642,7 +1642,7 @@ Bool_t PRunDataHandler::ReadRootFile()
// get all the data
Char_t histoName[32];
for (Int_t i=0; i<noOfHistos; i++) {
sprintf(histoName, "hDecay%02d", i);
snprintf(histoName, sizeof(histoName), "hDecay%02d", i);
TH1F *histo = dynamic_cast<TH1F*>(folder->FindObjectAny(histoName));
if (!histo) {
std::cerr << std::endl << ">> PRunDataHandler::ReadRootFile: **ERROR** Couldn't get histo " << histoName;
@@ -1671,14 +1671,14 @@ Bool_t PRunDataHandler::ReadRootFile()
histoData.clear();
}
// check if any post pileup histos are present at all (this is not the case for LEM data 2006 and earlier)
sprintf(histoName, "hDecay%02d", POST_PILEUP_HISTO_OFFSET);
snprintf(histoName, sizeof(histoName), "hDecay%02d", POST_PILEUP_HISTO_OFFSET);
if (!folder->FindObjectAny(histoName)) {
std::cerr << std::endl << ">> PRunDataHandler::ReadRootFile: **WARNING** Couldn't get histo " << histoName;
std::cerr << std::endl << ">> most probably this is an old (2006 or earlier) LEM file without post pileup histos.";
std::cerr << std::endl;
} else {
for (Int_t i=0; i<noOfHistos; i++) {
sprintf(histoName, "hDecay%02d", i+POST_PILEUP_HISTO_OFFSET);
snprintf(histoName, sizeof(histoName), "hDecay%02d", i+POST_PILEUP_HISTO_OFFSET);
TH1F *histo = dynamic_cast<TH1F*>(folder->FindObjectAny(histoName));
if (!histo) {
std::cerr << std::endl << ">> PRunDataHandler::ReadRootFile: **ERROR** Couldn't get histo " << histoName;
@@ -4676,7 +4676,7 @@ Bool_t PRunDataHandler::WriteRootFile(TString fln)
return false;
}
size = dataSet->GetData()->size();
sprintf(str, "hDecay%02d", static_cast<Int_t>(i));
snprintf(str, sizeof(str), "hDecay%02d", static_cast<Int_t>(i));
histo = new TH1F(str, str, size+1, -0.5, static_cast<Double_t>(size)+0.5);
for (UInt_t j=0; j<size; j++) {
histo->SetBinContent(j+1, dataSet->GetData()->at(j));
@@ -4694,7 +4694,7 @@ Bool_t PRunDataHandler::WriteRootFile(TString fln)
return false;
}
size = dataSet->GetData()->size();
sprintf(str, "hDecay%02d", static_cast<Int_t>(i));
snprintf(str, sizeof(str), "hDecay%02d", static_cast<Int_t>(i));
histo = new TH1F(str, str, static_cast<UInt_t>(size/fAny2ManyInfo->rebin)+1, -0.5, static_cast<Double_t>(size)/static_cast<Double_t>(fAny2ManyInfo->rebin)+0.5);
dataCount = 0;
for (UInt_t j=0; j<size; j++) {
@@ -5558,9 +5558,9 @@ Bool_t PRunDataHandler::WriteMudFile(TString fln)
MUD_setOrient(fd, (char *)fData[0].GetOrientation()->Data());
MUD_setDas(fd, dummy);
MUD_setExperimenter(fd, dummy);
sprintf(info, "%lf+-%lf (K)", fData[0].GetTemperature(0), fData[0].GetTempError(0));
snprintf(info, sizeof(info), "%lf+-%lf (K)", fData[0].GetTemperature(0), fData[0].GetTempError(0));
MUD_setTemperature(fd, info);
sprintf(info, "%lf", fData[0].GetField());
snprintf(info, sizeof(info), "%lf", fData[0].GetField());
MUD_setField(fd, info);
// generate the histograms
@@ -6219,7 +6219,7 @@ TString PRunDataHandler::FileNameFromTemplate(TString &fileNameTemplate, Int_t r
if (idx == str.Length()) { // 'r' only
TString runStr("");
char fmt[128];
sprintf(fmt , "%%0%dd", str.Length());
snprintf(fmt, sizeof(fmt), "%%0%dd", str.Length());
runStr.Form(fmt, run);
result += runStr;
} else { // not only 'r'

View File

@@ -114,7 +114,7 @@ PStartupHandler::PStartupHandler()
// check if the startup file is found under $HOME/.musrfit
home = getenv("HOME");
if (home != nullptr) {
sprintf(startup_path_name, "%s/.musrfit/musrfit_startup.xml", home);
snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit/musrfit_startup.xml", home);
if (StartupFileExists(startup_path_name)) {
fStartupFilePath = TString(startup_path_name);
fStartupFileFound = true;
@@ -125,7 +125,7 @@ PStartupHandler::PStartupHandler()
// check if the MUSRFITPATH system variable is set
pmusrpath = getenv("MUSRFITPATH");
if (pmusrpath != nullptr) {
sprintf(startup_path_name, "%s/musrfit_startup.xml", pmusrpath);
snprintf(startup_path_name, sizeof(startup_path_name), "%s/musrfit_startup.xml", pmusrpath);
if (StartupFileExists(startup_path_name)) {
fStartupFilePath = TString(startup_path_name);
fStartupFileFound = true;
@@ -135,9 +135,9 @@ PStartupHandler::PStartupHandler()
if (!fStartupFileFound) { // MUSRFITPATH not set or empty, will try $ROOTSYS/bin
home = getenv("ROOTSYS");
if (home != nullptr) {
sprintf(musrpath, "%s/bin", home);
snprintf(musrpath, sizeof(musrpath), "%s/bin", home);
std::cerr << std::endl << "**WARNING** MUSRFITPATH environment variable not set will try " << musrpath << std::endl;
sprintf(startup_path_name, "%s/musrfit_startup.xml", musrpath);
snprintf(startup_path_name, sizeof(startup_path_name), "%s/musrfit_startup.xml", musrpath);
if (StartupFileExists(startup_path_name)) {
fStartupFilePath = TString(startup_path_name);
fStartupFileFound = true;
@@ -153,7 +153,7 @@ PStartupHandler::PStartupHandler()
} else {
home = getenv("HOME");
if (home != nullptr) {
sprintf(startup_path_name, "%s/.musrfit/musrfit_startup.xml", home);
snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit/musrfit_startup.xml", home);
if (StartupFileExists(startup_path_name)) {
fStartupFilePath = TString(startup_path_name);
fStartupFileFound = true;
@@ -631,7 +631,7 @@ Bool_t PStartupHandler::WriteDefaultStartupFile()
// first check that $HOME/.musrfit exists and if NOT create it
struct stat info;
sprintf(startup_path_name, "%s/.musrfit", home);
snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit", home);
if (!stat(startup_path_name, &info)) {
if (!(info.st_mode & S_IFDIR))
return false;
@@ -643,7 +643,7 @@ Bool_t PStartupHandler::WriteDefaultStartupFile()
}
// set path-name for musrfit_startup.xml
sprintf(startup_path_name, "%s/.musrfit/musrfit_startup.xml", home);
snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit/musrfit_startup.xml", home);
std::ofstream fout(startup_path_name, std::ofstream::out);
if (!fout.is_open()) {

View File

@@ -841,12 +841,12 @@ void PTheory::MakeCleanAndTidyTheoryBlock(PMsrLines *fullTheoryBlock)
if (static_cast<UInt_t>(tokens->GetEntries()) < fgTheoDataBase[idx].fNoOfParam + 1)
return;
// make tidy string
sprintf(substr, "%-10s", fgTheoDataBase[idx].fName.Data());
snprintf(substr, sizeof(substr), "%-10s", fgTheoDataBase[idx].fName.Data());
tidy = TString(substr);
for (Int_t j=1; j<tokens->GetEntries(); j++) {
ostr = dynamic_cast<TObjString*>(tokens->At(j));
str = ostr->GetString();
sprintf(substr, "%6s", str.Data());
snprintf(substr, sizeof(substr), "%6s", str.Data());
tidy += TString(substr);
}
if (fgTheoDataBase[idx].fComment.Length() != 0) {
@@ -913,7 +913,7 @@ void PTheory::MakeCleanAndTidyPolynom(UInt_t i, PMsrLines *fullTheoryBlock)
for (Int_t j=1; j<max; j++) {
ostr = dynamic_cast<TObjString*>(tokens->At(j));
str = ostr->GetString();
sprintf(substr, "%6s", str.Data());
snprintf(substr, sizeof(substr), "%6s", str.Data());
tidy += TString(substr);
}