Skip to content

Commit

Permalink
Safer alternatives for "strncpy" (#2662)
Browse files Browse the repository at this point in the history
  • Loading branch information
kisslorand committed Apr 11, 2023
1 parent 60df3ef commit 910e3f0
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 42 deletions.
9 changes: 3 additions & 6 deletions TFT/src/User/API/Notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ void addToast(DIALOG_TYPE style, char * text)
LCD_WAKE();

TOAST t;
strncpy(t.text, text, TOAST_MSG_LENGTH);
t.text[TOAST_MSG_LENGTH - 1] = 0; // ensure string ends with null terminator
strxcpy(t.text, text, TOAST_MSG_LENGTH);
t.style = style;
t.isNew = true;
toastlist[nextToastIndex] = t;
Expand Down Expand Up @@ -147,10 +146,8 @@ void addNotification(DIALOG_TYPE style, char *title, char *text, bool ShowDialog

// store message
msglist[nextMsgIndex].style = style;
strncpy(msglist[nextMsgIndex].text, text, MAX_MSG_LENGTH);
msglist[nextMsgIndex].text[MAX_MSG_LENGTH - 1] = 0; // ensure string ends with null terminator
strncpy(msglist[nextMsgIndex].title, title, MAX_MSG_TITLE_LENGTH);
msglist[nextMsgIndex].title[MAX_MSG_TITLE_LENGTH - 1] = 0; // ensure string ends with null terminator
strxcpy(msglist[nextMsgIndex].text, text, MAX_MSG_LENGTH);
strxcpy(msglist[nextMsgIndex].title, title, MAX_MSG_TITLE_LENGTH);

if (ShowDialog && MENU_IS_NOT(menuNotification))
popupReminder(style, (uint8_t *)title, (uint8_t *)msglist[nextMsgIndex].text);
Expand Down
6 changes: 3 additions & 3 deletions TFT/src/User/API/Vfs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ bool addFile(bool isFile, const char * shortName, const char * longName)
if (sName == NULL) // in case of error, exit
return false;

strncpy(sName, shortName, sNameLen); // copy to "sName" and set to NULL the flag for filename extension check, if any
strxcpy(sName, shortName, sNameLen); // copy to "sName" and set to NULL the flag for filename extension check, if any

//
// get long name, if any
Expand All @@ -237,7 +237,7 @@ bool addFile(bool isFile, const char * shortName, const char * longName)
return false;
}

strncpy(lName, longName, lNameLen); // copy to "lName" and set to NULL the flag for filename extension check, if any
strxcpy(lName, longName, lNameLen); // copy to "lName" and set to NULL the flag for filename extension check, if any
}

//
Expand Down Expand Up @@ -357,7 +357,7 @@ bool getPrintTitle(char * buf, uint8_t len)
return false;
}

strncpy(buf, getFS(), len); // set source and set the flag for filename extension check
strxcpy(buf, getFS(), len); // set source and set the flag for filename extension check
strcat(buf, strPtr); // append filename
hideExtension(buf); // hide filename extension if filename extension feature is disabled

Expand Down
6 changes: 3 additions & 3 deletions TFT/src/User/API/interfaceCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ bool storeCmdFromUART(const CMD cmd, const SERIAL_PORT_INDEX portIndex)
return false;
}

strncpy(cmdQueue.queue[cmdQueue.index_w].gcode, cmd, CMD_MAX_SIZE);
strxcpy(cmdQueue.queue[cmdQueue.index_w].gcode, cmd, CMD_MAX_SIZE);

cmdQueue.queue[cmdQueue.index_w].port_index = portIndex;
cmdQueue.index_w = (cmdQueue.index_w + 1) % CMD_QUEUE_SIZE;
Expand Down Expand Up @@ -883,7 +883,7 @@ void sendQueueCmd(void)
bool hasE, hasA;

// make a copy to work on
strncpy(rawMsg, &cmd_ptr[cmd_base_index + 4], CMD_MAX_SIZE);
strxcpy(rawMsg, &cmd_ptr[cmd_base_index + 4], CMD_MAX_SIZE);

// retrieve message text and flags of M118 gcode
msgText = parseM118(rawMsg, &hasE, &hasA);
Expand Down Expand Up @@ -1030,7 +1030,7 @@ void sendQueueCmd(void)
const char * msgText;

// make a copy to work on
strncpy(rawMsg, &cmd_ptr[cmd_base_index + 4], CMD_MAX_SIZE);
strxcpy(rawMsg, &cmd_ptr[cmd_base_index + 4], CMD_MAX_SIZE);

// retrieve message text
stripChecksum(rawMsg);
Expand Down
2 changes: 1 addition & 1 deletion TFT/src/User/Fatfs/myfatfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ bool f_remove_full_dir(const TCHAR* path)
char dirBuffer[BUFFER_SIZE];
FILINFO tmpInfo;

strncpy(dirBuffer, path, BUFFER_SIZE);
strxcpy(dirBuffer, path, BUFFER_SIZE);
if (f_remove_node(dirBuffer, BUFFER_SIZE, &tmpInfo) == FR_OK)
{
return true;
Expand Down
29 changes: 8 additions & 21 deletions TFT/src/User/Menu/Popup.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,27 +124,14 @@ void menuDialog(void)
}
}

void popup_strcpy(uint8_t *dst, uint8_t *src, uint16_t size)
{
if (src)
{
strncpy((char *)dst, (char *)src, size);
dst[size - 1] = 0;
}
else
{
dst[0] = 0;
}
}

void _setDialogTitleStr(uint8_t * str)
{
popup_strcpy(popup_title, str, sizeof(popup_title));
strxcpy((char *)popup_title, (char *)str, sizeof(popup_title));
}

void _setDialogMsgStr(uint8_t * str)
{
popup_strcpy(popup_msg, str, sizeof(popup_msg));
strxcpy((char *)popup_msg, (char *)str, sizeof(popup_msg));
}

uint8_t *getDialogMsgStr()
Expand All @@ -154,40 +141,40 @@ uint8_t *getDialogMsgStr()

void _setDialogOkTextStr(uint8_t * str)
{
popup_strcpy(popup_ok, str, sizeof(popup_ok));
strxcpy((char *)popup_ok, (char *)str, sizeof(popup_ok));
}

void _setDialogCancelTextStr(uint8_t * str)
{
popup_strcpy(popup_cancel, str, sizeof(popup_cancel));
strxcpy((char *)popup_cancel, (char *)str, sizeof(popup_cancel));
}

void _setDialogTitleLabel(int16_t index)
{
uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0};
loadLabelText(tempstr, index);
popup_strcpy(popup_title, tempstr, sizeof(popup_title));
strxcpy((char *)popup_title, (char *)tempstr, sizeof(popup_title));
}

void _setDialogMsgLabel(int16_t index)
{
uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0};
loadLabelText(tempstr, index);
popup_strcpy(popup_msg, tempstr, sizeof(popup_msg));
strxcpy((char *)popup_msg, (char *)tempstr, sizeof(popup_msg));
}

void _setDialogOkTextLabel(int16_t index)
{
uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0};
loadLabelText(tempstr, index);
popup_strcpy(popup_ok, tempstr, sizeof(popup_ok));
strxcpy((char *)popup_ok, (char *)tempstr, sizeof(popup_ok));
}

void _setDialogCancelTextLabel(int16_t index)
{
uint8_t tempstr[MAX_LANG_LABEL_LENGTH] = {0};
loadLabelText(tempstr, index);
popup_strcpy(popup_cancel, tempstr, sizeof(popup_cancel));
strxcpy((char *)popup_cancel, (char *)tempstr, sizeof(popup_cancel));
}

/**
Expand Down
17 changes: 13 additions & 4 deletions TFT/src/User/Menu/PrintingMenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const char * const speedId[2] = {"Speed", "Flow "};

PROGRESS_DISPLAY progDisplayType;
LAYER_TYPE layerDisplayType;
char title[MAX_TITLE_LEN] = "";
char title[MAX_TITLE_LEN];

enum
{
Expand Down Expand Up @@ -129,12 +129,21 @@ static void setLayerNumberTxt(char * layer_number_txt)
}
}

// set print title according to print originator (remote or local to TFT)
static void setPrintTitle(void)
{
char * fileName = getPrintFilename();

hideExtension(fileName); // hide filename extension if filename extension feature is disabled
snprintf(title, MAX_TITLE_LEN, "%s%s", getFS(), fileName);
restoreExtension(fileName); // restore filename extension if filename extension feature is disabled
}

// initialize printing info before opening Printing menu
static void initMenuPrinting(void)
{
getPrintTitle(title, MAX_TITLE_LEN); // get print title according to print originator (remote or local to TFT)
clearInfoFile(); // as last, clear and free memory for file list

setPrintTitle(); // set print title according to print originator (remote or local to TFT)
clearInfoFile(); // as last, clear and free memory for file list
progDisplayType = infoSettings.prog_disp_type;

// layer number can be parsed only when TFT reads directly the G-code file
Expand Down
8 changes: 4 additions & 4 deletions TFT/src/User/Menu/StatusScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,17 @@ void drawStatus(void)

void statusScreen_setMsg(const uint8_t *title, const uint8_t *msg)
{
strncpy(msgTitle, (char *)title, sizeof(msgTitle));
strncpy(msgBody, (char *)msg, sizeof(msgBody));
strxcpy(msgTitle, (char *)title, sizeof(msgTitle));
strxcpy(msgBody, (char *)msg, sizeof(msgBody));
msgNeedRefresh = true;
}

void statusScreen_setReady(void)
{
strncpy(msgTitle, (char *)textSelect(LABEL_STATUS), sizeof(msgTitle));
strxcpy(msgTitle, (char *)textSelect(LABEL_STATUS), sizeof(msgTitle));

if (infoHost.connected == false)
strncpy(msgBody, (char *)textSelect(LABEL_UNCONNECTED), sizeof(msgBody));
strxcpy(msgBody, (char *)textSelect(LABEL_UNCONNECTED), sizeof(msgBody));
else
snprintf(msgBody, sizeof(msgBody), "%s %s", (char *)machine_type, (char *)textSelect(LABEL_READY));

Expand Down
47 changes: 47 additions & 0 deletions TFT/src/User/my_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,53 @@ uint32_t calculateCRC16(const uint8_t *data, uint32_t length)
return crc;
}

/*
* - always copy num-1 characters from source to destination
* regardless of null terminating character found in source
* - destination always ends with '\0'
*/
void strxcpy(char * destination, const char * source, size_t num)
{
num -= !!num;

memcpy(destination, source, num);
destination[num] ='\0';
}

/*
* - copy source to destination but no more than width-1 characters
* - if null terminating character found in source the rest is padded with 0
* - destination always ends with '\0'
*/
void strwcpy(char * destination, const char * source, size_t width)
{
width -= !!width;
while (width > 1 && *source != '\0')
{
*destination++ = *source++;
width--;
}

memset(destination, '\0', width);
}

/*
* - copy source to destination but no more than size-1 characters
* - if null terminating character found in source the copy stops there
* - destination always ends with '\0'
*/
void strscpy(char * destination, const char * source, size_t size)
{
size -= !!size;
while (size > 1 && *source != '\0')
{
*destination++ = *source++;
size--;
}

*destination = '\0';
}

// string convert to uint8, MSB
// "2C" to 0x2C
uint8_t string_2_uint8_t(const uint8_t *string)
Expand Down
9 changes: 9 additions & 0 deletions TFT/src/User/my_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extern "C" {

#include <stdbool.h>
#include <stdint.h>
#include <string.h>

// Menu Macros
#define OPEN_MENU(x) infoMenu.menu[++infoMenu.cur] = x
Expand Down Expand Up @@ -61,11 +62,19 @@ extern "C" {

#define strtod stringToDouble // enable light weight string to double function without exponential support

#define strncpy(...) \
do { \
_Pragma("GCC error \"Error: strncpy() is deprecated! Use the alternatives like strxcpy(), strwcpy() or strscpy().\""); \
} while (0)

uint8_t inRange(int cur, int tag , int range);
long map(long x, long in_min, long in_max, long out_min, long out_max);

uint32_t calculateCRC16(const uint8_t *data, uint32_t length); // Calculate CRC16 checksum

void strxcpy(char * destination, const char * source, size_t num);
void strwcpy(char * destination, const char * source, size_t num);
void strscpy(char * destination, const char * source, size_t num);
uint8_t string_2_uint8_t(const uint8_t *string);
uint8_t *uint8_2_string(uint8_t num, uint8_t *string);
uint32_t string_2_uint32(const uint8_t *string, const uint8_t bytes_num);
Expand Down

0 comments on commit 910e3f0

Please sign in to comment.