Skip to content

Commit

Permalink
WIP step 2
Browse files Browse the repository at this point in the history
Correction additionnelles pour MAC
- ajout de override
- remplacement systematique de tous les sprintf par des snprintf
- supression de la methode SecureStrcpy de standard, desormais inutile
  - ancien usage remplace par snprint

Corrction pour Ubuntu, pour la compilation de KWTest
- nettoyage de quelques lignes de code pour ne plus avoir d'erreurs
  • Loading branch information
marcboulle committed Oct 6, 2023
1 parent ec8bf0c commit 8d69f64
Show file tree
Hide file tree
Showing 32 changed files with 152 additions and 193 deletions.
2 changes: 1 addition & 1 deletion src/Learning/KWDRRuleLibrary/KWDRMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Symbol KWDRFormatContinuous::ComputeSymbolResult(const KWObject* kwoObject) cons
nLength--;
if (cValue < 0)
nLength++;
sprintf(sBuffer, "%0*.*f", nLength, nPrecision, (double)cValue);
snprintf(sBuffer, sizeof(sBuffer), "%0*.*f", nLength, nPrecision, (double)cValue);
return (Symbol)sBuffer;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Learning/KWData/KWCYac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3215,7 +3215,7 @@ void yyerrorWithLineCorrection(char const* fmt, int nDeltaLineNumber)
nLineNumber = yylineno + nDeltaLineNumber;
if (nLineNumber <= 0)
nLineNumber = 1;
sprintf(sErrorLine, "Line %d", nLineNumber);
snprintf(sErrorLine, sizeof(sErrorLine), "Line %d", nLineNumber);
sLabel = fmt;
Global::AddError("Read dictionary file", sErrorLine, sLabel);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Learning/KWData/KWCYac.yac
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ void yyerrorWithLineCorrection(char const *fmt, int nDeltaLineNumber)
nLineNumber = yylineno+nDeltaLineNumber;
if (nLineNumber <= 0)
nLineNumber = 1;
sprintf(sErrorLine, "Line %d", nLineNumber);
snprintf(sErrorLine, sErrorLine, "Line %d", nLineNumber);
sLabel = fmt;
Global::AddError("Read dictionary file",
sErrorLine,
Expand Down
14 changes: 7 additions & 7 deletions src/Learning/KWData/KWContinuous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,7 @@ const char* const KWContinuous::StandardContinuousToString(Continuous cValue)
if (cValue == GetMissingValue())
sBuffer[0] = '\0';
else
sprintf(sBuffer, "%.10g", cValue);
snprintf(sBuffer, BUFFER_LENGTH, "%.10g", cValue);
return sBuffer;
}

Expand Down Expand Up @@ -1860,7 +1860,7 @@ void KWContinuous::CompareStringToContinuous(Continuous cValue, boolean bShow)
return;

// Conversion
sprintf(sValue, "%.15g", cValue);
snprintf(sValue, sizeof(sValue), "%.15g", cValue);
dRefValue = StandardStringToContinuous(sValue);
dNewValue = StringToContinuous(sValue);

Expand All @@ -1884,7 +1884,7 @@ const char* const KWContinuous::MaxPrecisionDoubleToString(double dValue)
{
char* sBuffer = StandardGetBuffer();

sprintf(sBuffer, "%.15g", dValue);
snprintf(sBuffer, BUFFER_LENGTH, "%.15g", dValue);

return sBuffer;
}
Expand Down Expand Up @@ -1939,7 +1939,7 @@ void KWContinuous::TestPerformanceStringToContinuous(int nMaxLowerBaseValue, dou
{
// Nombre positif avec exposant positif
cValue = (cUpperBaseValue + nLowerBaseValue) * dPositivePower10[nExponent];
sprintf(sValue, "%.15g", cValue);
snprintf(sValue, sizeof(sValue), "%.15g", cValue);
if (bRefConversion)
StandardStringToContinuous(sValue);
if (bNewConversion)
Expand All @@ -1949,7 +1949,7 @@ void KWContinuous::TestPerformanceStringToContinuous(int nMaxLowerBaseValue, dou

// Nombre positif avec exposant negatif
cValue = (cUpperBaseValue + nLowerBaseValue) * dNegativePower10[nExponent];
sprintf(sValue, "%.15g", cValue);
snprintf(sValue, sizeof(sValue), "%.15g", cValue);
if (bRefConversion)
StandardStringToContinuous(sValue);
if (bNewConversion)
Expand All @@ -1959,7 +1959,7 @@ void KWContinuous::TestPerformanceStringToContinuous(int nMaxLowerBaseValue, dou

// Nombre negatif avec exposant positif
cValue = -(cUpperBaseValue + nLowerBaseValue) * dPositivePower10[nExponent];
sprintf(sValue, "%.15g", cValue);
snprintf(sValue, sizeof(sValue), "%.15g", cValue);
if (bRefConversion)
StandardStringToContinuous(sValue);
if (bNewConversion)
Expand All @@ -1969,7 +1969,7 @@ void KWContinuous::TestPerformanceStringToContinuous(int nMaxLowerBaseValue, dou

// Nombre negatif avec exposant negatif
cValue = -(cUpperBaseValue + nLowerBaseValue) * dNegativePower10[nExponent];
sprintf(sValue, "%.15g", cValue);
snprintf(sValue, sizeof(sValue), "%.15g", cValue);
if (bRefConversion)
StandardStringToContinuous(sValue);
if (bNewConversion)
Expand Down
26 changes: 14 additions & 12 deletions src/Learning/KWData/KWDate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const char* const Date::ToString() const
if (not Check())
sDate[0] = '\0';
else
sprintf(sDate, "%04d-%02d-%02d", GetYear(), GetMonth(), GetDay());
snprintf(sDate, BUFFER_LENGTH, "%04d-%02d-%02d", GetYear(), GetMonth(), GetDay());
return sDate;
}

Expand Down Expand Up @@ -320,9 +320,11 @@ const char* const Date::TimeZoneToString(boolean bExtended) const
else
cSign = '-';
if (bExtended)
sprintf(sTimeZone, "%c%02d:%02d", cSign, GetTimeZoneHour(), GetTimeZoneMinute());
snprintf(sTimeZone, BUFFER_LENGTH, "%c%02d:%02d", cSign, GetTimeZoneHour(),
GetTimeZoneMinute());
else
sprintf(sTimeZone, "%c%02d%02d", cSign, GetTimeZoneHour(), GetTimeZoneMinute());
snprintf(sTimeZone, BUFFER_LENGTH, "%c%02d%02d", cSign, GetTimeZoneHour(),
GetTimeZoneMinute());
}
}
return sTimeZone;
Expand Down Expand Up @@ -618,11 +620,11 @@ const char* const KWDateFormat::DateToString(Date dtValue) const
nOffset = 0;
assert(nOffset == nYearOffset or nOffset == nMonthOffset or nOffset == nDayOffset);
if (nYearOffset == 0)
nOffset += sprintf(sBuffer, "%04d", dtValue.GetYear());
nOffset += snprintf(sBuffer, BUFFER_LENGTH, "%04d", dtValue.GetYear());
else if (nMonthOffset == 0)
nOffset += sprintf(sBuffer, "%02d", dtValue.GetMonth());
nOffset += snprintf(sBuffer, BUFFER_LENGTH, "%02d", dtValue.GetMonth());
else
nOffset += sprintf(sBuffer, "%02d", dtValue.GetDay());
nOffset += snprintf(sBuffer, BUFFER_LENGTH, "%02d", dtValue.GetDay());

// Ecriture du premier separateur optionnel
if (nSeparatorOffset1 == nOffset)
Expand All @@ -634,11 +636,11 @@ const char* const KWDateFormat::DateToString(Date dtValue) const
// Ecriture du deuxieme champ
assert(nOffset == nYearOffset or nOffset == nMonthOffset or nOffset == nDayOffset);
if (nYearOffset == nOffset)
nOffset += sprintf(sBuffer + nOffset, "%04d", dtValue.GetYear());
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%04d", dtValue.GetYear());
else if (nMonthOffset == nOffset)
nOffset += sprintf(sBuffer + nOffset, "%02d", dtValue.GetMonth());
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%02d", dtValue.GetMonth());
else
nOffset += sprintf(sBuffer + nOffset, "%02d", dtValue.GetDay());
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%02d", dtValue.GetDay());

// Ecriture du deuxiemme separateur optionnel
if (nSeparatorOffset2 == nOffset)
Expand All @@ -650,11 +652,11 @@ const char* const KWDateFormat::DateToString(Date dtValue) const
// Ecriture du troisieme champ
assert(nOffset == nYearOffset or nOffset == nMonthOffset or nOffset == nDayOffset);
if (nYearOffset == nOffset)
nOffset += sprintf(sBuffer + nOffset, "%04d", dtValue.GetYear());
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%04d", dtValue.GetYear());
else if (nMonthOffset == nOffset)
nOffset += sprintf(sBuffer + nOffset, "%02d", dtValue.GetMonth());
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%02d", dtValue.GetMonth());
else
nOffset += sprintf(sBuffer + nOffset, "%02d", dtValue.GetDay());
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%02d", dtValue.GetDay());
}
return sBuffer;
}
Expand Down
23 changes: 13 additions & 10 deletions src/Learning/KWData/KWTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ const char* const Time::ToString() const

// Cas ou il n'y a pas de fraction de secondes
if (nSecondFrac == 0)
sprintf(sTime, "%02d:%02d:%02d", GetHour(), GetMinute(), (int)timeValue.timeFields.nSecond);
snprintf(sTime, BUFFER_LENGTH, "%02d:%02d:%02d", GetHour(), GetMinute(),
(int)timeValue.timeFields.nSecond);
// Cas avec fraction de secondes
else
{
nLength = sprintf(sTime, "%02d:%02d:%02d.%04d", GetHour(), GetMinute(),
(int)timeValue.timeFields.nSecond, nSecondFrac);
nLength = snprintf(sTime, BUFFER_LENGTH, "%02d:%02d:%02d.%04d", GetHour(), GetMinute(),
(int)timeValue.timeFields.nSecond, nSecondFrac);

// Supression des zero en fin pour ne garder que la partie utile des decimales de secondes
for (i = 0; i < 4; i++)
Expand Down Expand Up @@ -660,9 +661,9 @@ const char* const KWTimeFormat::TimeToString(Time tmValue) const
nOffset = 0;
assert(nOffset == nHourOffset);
if (bMandatoryFirstDigit)
nOffset += sprintf(sBuffer, "%02d", tmValue.GetHour());
nOffset += snprintf(sBuffer, BUFFER_LENGTH, "%02d", tmValue.GetHour());
else
nOffset += sprintf(sBuffer, "%d", tmValue.GetHour());
nOffset += snprintf(sBuffer, BUFFER_LENGTH, "%d", tmValue.GetHour());

// Ecriture du premier separateur optionnel
if (nSeparatorOffset1 != -1)
Expand All @@ -673,9 +674,9 @@ const char* const KWTimeFormat::TimeToString(Time tmValue) const

// Ecriture du champ minute
if (bMandatoryFirstDigit)
nOffset += sprintf(sBuffer + nOffset, "%02d", tmValue.GetMinute());
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%02d", tmValue.GetMinute());
else
nOffset += sprintf(sBuffer + nOffset, "%d", tmValue.GetMinute());
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%d", tmValue.GetMinute());

// Ecriture du deuxieme separateur optionnel
if (nSeparatorOffset2 != -1)
Expand All @@ -693,9 +694,11 @@ const char* const KWTimeFormat::TimeToString(Time tmValue) const
if (nSecondOffset != -1)
{
if (bMandatoryFirstDigit)
nOffset += sprintf(sBuffer + nOffset, "%02d", (int)floor(tmValue.GetSecond()));
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%02d",
(int)floor(tmValue.GetSecond()));
else
nOffset += sprintf(sBuffer + nOffset, "%d", (int)floor(tmValue.GetSecond()));
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, "%d",
(int)floor(tmValue.GetSecond()));
}

// Ecriture de la partie decimale des secondes
Expand All @@ -709,7 +712,7 @@ const char* const KWTimeFormat::TimeToString(Time tmValue) const
// Ecriture des fractions de secondes
if (nSecondFrac > 0)
{
nOffset += sprintf(sBuffer + nOffset, ".%04d", nSecondFrac);
nOffset += snprintf(sBuffer + nOffset, BUFFER_LENGTH - nOffset, ".%04d", nSecondFrac);

// Supression des zero en fin pour ne garder que la partie utile des decimales de
// secondes
Expand Down
2 changes: 1 addition & 1 deletion src/Learning/KWData/KWTimestamp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const char* const Timestamp::ToString() const
if (not Check())
sTimestamp[0] = '\0';
else
sprintf(sTimestamp, "%s %s", GetDate().ToString(), GetTime().ToString());
snprintf(sTimestamp, BUFFER_LENGTH, "%s %s", GetDate().ToString(), GetTime().ToString());
return sTimestamp;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Learning/KWData/KWTimestampTZ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ const char* const TimestampTZ::ToString() const
if (not Check())
sTimestampTZ[0] = '\0';
else
sprintf(sTimestampTZ, "%s %s%s", GetInternalDate().ToString(), GetInternalTime().ToString(),
GetInternalDate().TimeZoneToString(true));
snprintf(sTimestampTZ, BUFFER_LENGTH, "%s %s%s", GetInternalDate().ToString(),
GetInternalTime().ToString(), GetInternalDate().TimeZoneToString(true));
return sTimestampTZ;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Learning/KWTest/CreateLargeFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ boolean MovePositionInBinaryFile(FILE* fFile, longint lOffset)
boolean bOk;

// Pour les fichiers de plus de 4 Go, il existe une API speciale (stat64...)
#if defined __MSC__
#ifdef _WIN32
_fseeki64(fFile, lOffset, SEEK_CUR);
#elif defined(__APPLE__)
fseeko(fFile, lOffset, SEEK_CUR);
Expand All @@ -25,7 +25,7 @@ longint TellPositionInBinaryFile(FILE* fFile)
longint lCurrentPosition;

// Pour les fichiers de plus de 4 Go, il existe une API speciale (stat64...)
#if defined __MSC__
#ifdef _WIN32
lCurrentPosition = _ftelli64(fFile);
#elif defined(__APPLE__)
lCurrentPosition = ftello(fFile);
Expand Down
21 changes: 7 additions & 14 deletions src/Learning/KWTest/SparseStudy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,6 @@ void BasicVectorAccessTest()
}
}

#include <chrono>
using namespace std::chrono;

void DoubleVectorAccessTest()
{
const int nSize = 1000;
Expand All @@ -264,16 +261,14 @@ void DoubleVectorAccessTest()
int i;
double dTotal;
char* dvVectorAdress;
std::chrono::time_point<std::chrono::steady_clock> start;
std::chrono::time_point<std::chrono::steady_clock> stop;
double dDuration;
Timer timer;

// Initialisation
for (i = 0; i < nSize; i++)
dvVector[i] = i;

// Lecture
start = high_resolution_clock::now();
timer.Start();
dTotal = 0;
for (nStep = 0; nStep < nStepNumber; nStep++)
{
Expand All @@ -282,14 +277,13 @@ void DoubleVectorAccessTest()
dTotal += dvVector[i];
}
}
stop = high_resolution_clock::now();
dDuration = duration_cast<chrono::nanoseconds>(stop - start).count() / 1000000000.0;
timer.Stop();
cout << "Total: " << dTotal << endl;
cout << "Duration: " << dDuration << endl;
cout << "Duration: " << timer.GetElapsedTime() << endl;

// Lecture
dvVectorAdress = (char*)(void*)&dvVector;
start = high_resolution_clock::now();
timer.Start();
dTotal = 0;
for (nStep = 0; nStep < nStepNumber; nStep++)
{
Expand All @@ -298,10 +292,9 @@ void DoubleVectorAccessTest()
dTotal += *((double*)&dvVectorAdress[i * 8]);
}
}
stop = high_resolution_clock::now();
dDuration = duration_cast<chrono::nanoseconds>(stop - start).count() / 1000000000.0;
timer.Stop();
cout << "Total: " << dTotal << endl;
cout << "Duration: " << dDuration << endl;
cout << "Duration: " << timer.GetElapsedTime() << endl;
}

/////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 7 additions & 2 deletions src/Learning/KWTest/SparseStudy.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void DoubleVectorAccessTest();
// Classe KWValueIndexPair
// Class technique, utilisee uniquement pour l'implementation des blocs sparse
// Pas de "packing" des attribut pour optimiser la place memoire
#ifdef _WIN32
// #ifdef _WIN32
#pragma pack(push)
#pragma pack(1)
class KWValueIndexPairTest : public SystemObject
Expand All @@ -117,14 +117,19 @@ class KWValueIndexPairTest : public SystemObject
friend class KWContinuousValueBlockTest;
};
#pragma pack(pop)
// Sous linux on doit utiliser __attribute__((packed)); mais pragma pack fonctionne sur clang et gcc, on privilegie donc
// la version portable. ATTENTION il faut declarer nIndex avant value sinon le packing n'est pas fait sur linux (on ne
// sait pas pourquoi)

/*
#else // Linux
class KWValueIndexPairTest : public SystemObject
{
KWValue value;
int nIndex;
friend class KWContinuousValueBlockTest;
} __attribute__((packed));
#endif
#endif*/

/////////////////////////////////////////////////////////////////////
// Classe KWContinuousValueBlock: gestion d'un bloc de valeurs sparse
Expand Down
5 changes: 3 additions & 2 deletions src/Learning/KWTest/StreamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// at https://spdx.org/licenses/BSD-3-Clause-Clear.html or see the "LICENSE" file for more details.

#include "StreamTest.h"

#ifdef _WIN32
#include <fcntl.h>
#endif // _WIN32

// Visual C++: supression des Warning sur les assignation au sein d'un expression conditionnelle (cf.
// TestStreamLineCount7 et TestStreamLineCount8)
#ifdef __MSC__
#pragma warning(disable : 4706) // disable C4706 warning
#endif
#endif __MSC__
#endif // __MSC__

void StreamTest(int argc, char** argv)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Learning/KWTest/SystemDivers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int OpenPdfFile(char* sPdfFilePathName)
return ((int)nRet>32);
*/
char sCommand[1000];
sprintf(sCommand, "start %s", sPdfFilePathName);
snprintf(sCommand, sizeof(sCommand), "start %s", sPdfFilePathName);
system(sCommand);
return 0;
}
2 changes: 1 addition & 1 deletion src/Learning/MHHistograms/MHHistogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class MHHistogram : public Object
virtual void CopyFrom(const MHHistogram* sourceHistogram);

// Duplication
virtual MHHistogram* Clone() const;
MHHistogram* Clone() const;

// Ecriture sous forme d'un tableau de bins avec leurs caracteristiques
virtual void WriteSummary(ostream& ost) const;
Expand Down
2 changes: 1 addition & 1 deletion src/Learning/genum/MHEnumHistogramCost.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MHEnumHistogramCosts : public MHGenumHistogramCosts
double ComputePartCost(const KWFrequencyVector* part) const override;

// Calcul du cout global de la partition, definie par le tableau de ses parties
double ComputePartitionGlobalCost(const KWFrequencyTable* partTable) const;
double ComputePartitionGlobalCost(const KWFrequencyTable* partTable) const override;

// Affichage du cout de la partition
void WritePartitionCost(int nPartNumber, int nGarbageModalityNumber, ostream& ost) const override;
Expand Down
Loading

0 comments on commit 8d69f64

Please sign in to comment.