Skip to content

Commit

Permalink
#92 (strcat -> strncat)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jan 29, 2021
1 parent c2536b2 commit d1896fe
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 56 deletions.
12 changes: 6 additions & 6 deletions src/eez/gui/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void FLOAT_value_to_text(const Value &value, char *text, int count) {

#if defined(INFINITY_SYMBOL)
if (isinf(floatValue)) {
strcat(text, INFINITY_SYMBOL);
snprintf(text, count, INFINITY_SYMBOL);
return;
}
#endif
Expand Down Expand Up @@ -172,7 +172,7 @@ void FLOAT_value_to_text(const Value &value, char *text, int count) {

if (!isNaN(floatValue)) {
if ((value.getOptions() & FLOAT_OPTIONS_LESS_THEN) != 0) {
strcat(text, "< ");
strncat(text, "< ", count - strlen(text) - 1);
}

if (fixedDecimals) {
Expand All @@ -196,12 +196,12 @@ void FLOAT_value_to_text(const Value &value, char *text, int count) {
if (decimalPointIndex == n) {
if (appendDotZero) {
// 1 => 1.0
strcat(text, ".0");
strncat(text, ".0", count - strlen(text) - 1);
}
} else if (decimalPointIndex == n - 1) {
if (appendDotZero) {
// 1. => 1.0
strcat(text, "0");
strncat(text, "0", count - strlen(text) - 1);
} else {
text[decimalPointIndex] = 0;
}
Expand All @@ -219,8 +219,8 @@ void FLOAT_value_to_text(const Value &value, char *text, int count) {
}
}

strcat(text, " ");
strcat(text, getUnitName(unit));
strncat(text, " ", count - strlen(text) - 1);
strncat(text, getUnitName(unit), count - strlen(text) - 1);
} else {
text[0] = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/eez/modules/psu/gui/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1975,8 +1975,8 @@ void data_firmware_info(DataOperationEnum operation, Cursor cursor, Value &value
static char firmware_info[sizeof(FIRMWARE_LABEL) - 1 + sizeof(MCU_FIRMWARE) - 1 + 1];

if (*firmware_info == 0) {
strcat(firmware_info, FIRMWARE_LABEL);
strcat(firmware_info, MCU_FIRMWARE);
strncat(firmware_info, FIRMWARE_LABEL, sizeof(firmware_info) - strlen(firmware_info) - 1);
strncat(firmware_info, MCU_FIRMWARE, sizeof(firmware_info) - strlen(firmware_info) - 1);
}

value = firmware_info;
Expand Down
10 changes: 5 additions & 5 deletions src/eez/modules/psu/gui/edit_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,16 @@ void getInfoText(char *infoText, int count) {
snprintf(infoText, count, "Set Ch%d ", getFocusCursor() + 1);
}

strcat(infoText, dataName);
strncat(infoText, dataName, count - strlen(infoText) - 1);
} else {
strcat(infoText, dataName);
strncat(infoText, dataName, count - strlen(infoText) - 1);
}

strcat(infoText, " [");
strncat(infoText, " [", count - strlen(infoText) - 1);
minValue.toText(infoText + strlen(infoText), count - strlen(infoText));
strcat(infoText, " - ");
strncat(infoText, " - ", count - strlen(infoText) - 1);
maxValue.toText(infoText + strlen(infoText), count - strlen(infoText));
strcat(infoText, "]");
strncat(infoText, "]", count - strlen(infoText) - 1);
}

static void update() {
Expand Down
14 changes: 7 additions & 7 deletions src/eez/modules/psu/gui/file_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ bool makeAbsolutePath(const char *relativePath, char *dest) {
dest[1] = ':';
strcpy(dest + 2, g_currentDirectory);
if (relativePath) {
strcat(dest, "/");
strcat(dest, relativePath);
strncat(dest, "/", MAX_PATH_LENGTH - strlen(dest) - 1);
strncat(dest, relativePath, MAX_PATH_LENGTH - strlen(dest) - 1);
}
return true;
}
Expand Down Expand Up @@ -717,8 +717,8 @@ void selectFile(uint32_t fileIndex) {
animateLoadDirectory();
} else if (fileItem && fileItem->type == FILE_TYPE_DIRECTORY) {
if (2 + strlen(g_currentDirectory) + 1 + strlen(fileItem->name) <= MAX_PATH_LENGTH) {
strcat(g_currentDirectory, "/");
strcat(g_currentDirectory, fileItem->name);
strncat(g_currentDirectory, "/", MAX_PATH_LENGTH - strlen(g_currentDirectory) - 1);
strncat(g_currentDirectory, fileItem->name, MAX_PATH_LENGTH - strlen(g_currentDirectory) - 1);
g_filesStartPosition = 0;
loadDirectory();
animateLoadDirectory();
Expand All @@ -731,7 +731,7 @@ void selectFile(uint32_t fileIndex) {
if (strlen(fileItem->name) + 3 <= MAX_PATH_LENGTH) {
char fileName[MAX_PATH_LENGTH + 1];
strcpy(fileName, fileItem->name);
strcat(fileName, ".py");
strncat(fileName, ".py", MAX_PATH_LENGTH - strlen(fileName) - 1);
char filePath[MAX_PATH_LENGTH + 1];
if (makeAbsolutePath(fileName, filePath)) {
mp::startScript(filePath);
Expand Down Expand Up @@ -939,7 +939,7 @@ void doRenameFile() {

strcpy(dstFileName, g_fileNameWithoutExtension);
if (extension) {
strcat(dstFileName, extension);
strncat(dstFileName, extension, MAX_PATH_LENGTH - strlen(dstFileName) - 1);
}

char dstFilePath[MAX_PATH_LENGTH + 1];
Expand Down Expand Up @@ -1082,7 +1082,7 @@ void onNewFileOk(char *fileNameWithoutExtension) {
}

strcpy(fileName, fileNameWithoutExtension);
strcat(fileName, extension);
strncat(fileName, extension, MAX_PATH_LENGTH - strlen(fileName) - 1);

char filePath[MAX_PATH_LENGTH + 1];
if (!makeAbsolutePath(fileName, filePath)) {
Expand Down
8 changes: 4 additions & 4 deletions src/eez/modules/psu/gui/keypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,9 @@ char NumericKeypad::getDotSign() {
return '.';
}

void NumericKeypad::appendEditUnit(char *text) {
strcat(text, " ");
strcat(text, getUnitName(m_options.editValueUnit));
void NumericKeypad::appendEditUnit(char *text, size_t maxTextLength) {
strncat(text, " ", maxTextLength - strlen(text) - 1);
strncat(text, getUnitName(m_options.editValueUnit), maxTextLength - strlen(text) - 1);
}

void NumericKeypad::getKeypadText(char *text) {
Expand All @@ -515,7 +515,7 @@ bool NumericKeypad::getText(char *text, int count) {

strcpy(text, m_keypadText);

appendEditUnit(text);
appendEditUnit(text, count);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/eez/modules/psu/gui/keypad.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class NumericKeypad : public Keypad {
void (*m_okUint32Callback)(uint32_t);
void (*m_cancelCallback)();

void appendEditUnit(char *text);
void appendEditUnit(char *text, size_t maxTextLength);
double getValue();
char getDotSign();

Expand Down
8 changes: 4 additions & 4 deletions src/eez/modules/psu/gui/page_ch_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,21 +726,21 @@ void ChSettingsListsPage::edit() {
if (dataId == DATA_ID_CHANNEL_LIST_DWELL) {
char dwell[64];
min.toText(dwell, sizeof(dwell));
strcat(label, dwell);
strncat(label, dwell, sizeof(label) - strlen(label) - 1);
} else {
strcatFloat(label, sizeof(label), options.min);
}
strcat(label, "-");
strncat(label, "-", sizeof(label) - strlen(label) - 1);
if (dataId == DATA_ID_CHANNEL_LIST_DWELL) {
char dwell[64];
max.toText(dwell, sizeof(dwell));
strcat(label, dwell);
strncat(label, dwell, sizeof(label) - strlen(label) - 1);
} else if (dataId == DATA_ID_CHANNEL_LIST_VOLTAGE) {
strcatVoltage(label, sizeof(label), options.max);
} else {
strcatCurrent(label, sizeof(label), options.max);
}
strcat(label, "]: ");
strncat(label, "]: ", sizeof(label) - strlen(label) - 1);

NumericKeypad::start(label, value, options, onValueSet, 0, 0);
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/eez/modules/psu/profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,8 @@ static Parameters *getProfileParametersFromCache(int location) {
////////////////////////////////////////////////////////////////////////////////

static void getProfileFilePath(int location, char *filePath, size_t filePathStrLength) {
strcpy(filePath, PROFILES_DIR);
strcat(filePath, PATH_SEPARATOR);
strcatInt(filePath, filePathStrLength - strlen(filePath), location);
strcat(filePath, getExtensionFromFileType(FILE_TYPE_PROFILE));
snprintf(filePath, filePathStrLength, "%s%s%d%s",
PROFILES_DIR, PATH_SEPARATOR, location, getExtensionFromFileType(FILE_TYPE_PROFILE));
}

static void resetProfileToDefaults(Parameters &profile) {
Expand Down
12 changes: 6 additions & 6 deletions src/eez/modules/psu/psu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,22 @@ char *getConfFilePath(const char *file_name) {

#ifdef _WIN32
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, file_path))) {
strcat(file_path, "\\.eez_psu_sim");
strncat(file_path, "\\.eez_psu_sim", sizeof(file_path) - strlen(file_path) - 1);
_mkdir(file_path);
strcat(file_path, "\\");
strncat(file_path, "\\", sizeof(file_path) - strlen(file_path) - 1);
}
#elif defined(__EMSCRIPTEN__)
strcat(file_path, "/eez_modular_firmware/");
strncat(file_path, "/eez_modular_firmware/", sizeof(file_path) - strlen(file_path) - 1);
#else
const char *home_dir = 0;
if ((home_dir = getenv("HOME")) == NULL) {
home_dir = getpwuid(getuid())->pw_dir;
}
if (home_dir) {
strcat(file_path, home_dir);
strcat(file_path, "/.eez_psu_sim");
strncat(file_path, home_dir, sizeof(file_path) - strlen(file_path) - 1);
strncat(file_path, "/.eez_psu_sim", sizeof(file_path) - strlen(file_path) - 1);
mkdir(file_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
strcat(file_path, "/");
strncat(file_path, "/", sizeof(file_path) - strlen(file_path) - 1);
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/eez/modules/psu/scpi/appl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ scpi_result_t scpi_cmd_applyQ(scpi_t *context) {
// return both current and voltage
snprintf(buffer, sizeof(buffer), "CH%d:", channel->channelIndex + 1);
strcatVoltage(buffer, sizeof(buffer), channel_dispatcher::getUMax(*channel));
strcat(buffer, "/");
strncat(buffer, "/", sizeof(buffer) - strlen(buffer) - 1);
strcatCurrent(buffer, sizeof(buffer), channel_dispatcher::getIMax(*channel));
strcat(buffer, ", ");
strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1);

strcatFloat(buffer, sizeof(buffer), channel_dispatcher::getUSet(*channel));
strcat(buffer, ", ");
strncat(buffer, ", ", sizeof(buffer) - strlen(buffer) - 1);
strcatFloat(buffer, sizeof(buffer), channel_dispatcher::getISet(*channel));
} else {
if (current_or_voltage == 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/eez/modules/psu/scpi/diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ scpi_result_t scpi_cmd_diagnosticInformationProtectionQ(scpi_t *context) {

snprintf(buffer, sizeof(buffer), "temp_%s_level=", sensor.name);
strcatFloat(buffer, sizeof(buffer), sensorTemperature.prot_conf.level);
strcat(buffer, " oC");
strncat(buffer, " oC", sizeof(buffer) - strlen(buffer) - 1);
SCPI_ResultText(context, buffer);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/eez/modules/psu/scpi/mmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace scpi {

void addExtension(char *filePath, const char *ext) {
if (!endsWith(filePath, ext)) {
strcat(filePath, ext);
strncat(filePath, ext, MAX_PATH_LENGTH - strlen(filePath) - 1);
}
}

Expand Down
24 changes: 12 additions & 12 deletions src/eez/modules/psu/scpi/syst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,42 +710,42 @@ scpi_result_t scpi_cmd_systemCpuOptionQ(scpi_t *context) {

#if OPTION_BP
if (strFeatures[0]) {
strcat(strFeatures, ", ");
strncat(strFeatures, ", ", sizeof(strFeatures) - strlen(strFeatures) - 1);
}
strcat(strFeatures, "BPost");
strncat(strFeatures, "BPost", sizeof(strFeatures) - strlen(strFeatures) - 1);
#endif

#if OPTION_EXT_EEPROM
if (strFeatures[0]) {
strcat(strFeatures, ", ");
strncat(strFeatures, ", ", sizeof(strFeatures) - strlen(strFeatures) - 1);
}
strcat(strFeatures, "EEPROM");
strncat(strFeatures, "EEPROM", sizeof(strFeatures) - strlen(strFeatures) - 1);
#endif

#if OPTION_EXT_RTC
if (strFeatures[0]) {
strcat(strFeatures, ", ");
strncat(strFeatures, ", ", sizeof(strFeatures) - strlen(strFeatures) - 1);
}
strcat(strFeatures, "RTC");
strncat(strFeatures, "RTC", sizeof(strFeatures) - strlen(strFeatures) - 1);
#endif

if (strFeatures[0]) {
strcat(strFeatures, ", ");
strncat(strFeatures, ", ", sizeof(strFeatures) - strlen(strFeatures) - 1);
}
strcat(strFeatures, "SDcard");
strncat(strFeatures, "SDcard", sizeof(strFeatures) - strlen(strFeatures) - 1);

#if OPTION_ETHERNET
if (strFeatures[0]) {
strcat(strFeatures, ", ");
strncat(strFeatures, ", ", sizeof(strFeatures) - strlen(strFeatures) - 1);
}
strcat(strFeatures, "Ethernet");
strncat(strFeatures, "Ethernet", sizeof(strFeatures) - strlen(strFeatures) - 1);
#endif

#if OPTION_DISPLAY
if (strFeatures[0]) {
strcat(strFeatures, ", ");
strncat(strFeatures, ", ", sizeof(strFeatures) - strlen(strFeatures) - 1);
}
strcat(strFeatures, "Display");
strncat(strFeatures, "Display", sizeof(strFeatures) - strlen(strFeatures) - 1);
#endif

SCPI_ResultText(context, strFeatures);
Expand Down

0 comments on commit d1896fe

Please sign in to comment.