I have the following functions: static int InitExtraFolders(void) { struct stat…
- User
I have the following functions:
static int InitExtraFolders(void) { struct stat stat_buffer; struct _finddata_t files; int count = 0; char buf[256]; char curdir[MAX_PATH]; const std::string t = dir_get_value(24); const char dir = t.c_str(); memset(ExtraFolderData, 0, (MAX_EXTRA_FOLDERS * MAX_EXTRA_SUBFOLDERS) sizeof(LPEXFOLDERDATA));
/* NPW 9-Feb-2003 - MSVC stat() doesn't like stat() called with an empty string */ if (!dir) return 0; // Why create the directory if it doesn't exist, just return 0 folders. if (stat(dir, &stat_buffer) != 0) return 0; _getcwd(curdir, MAX_PATH); chdir(dir); for (int i = 0; i < MAX_EXTRA_FOLDERS; i++) { ExtraFolderIcons[i] = NULL; } numExtraIcons = 0; intptr_t hLong = 0L; if ( (hLong = _findfirst("*.ini", &files)) == -1L ) return 0; do { if ((files.attrib & _A_SUBDIR) == 0) { FILE *fp; fp = fopen(files.name, "r"); if (fp != NULL) { int icon[2] = { 0, 0 }; while (fgets(buf, 256, fp)) { if (buf[0] == '[') { char *p = strchr(buf, ']'); if (p == NULL) continue; *p = '\0'; char *name = &buf[1]; if (!strcmp(name, "FOLDER_SETTINGS")) { while (fgets(buf, 256, fp)) { name = strtok(buf, " =\r\n"); if (name == NULL) break; if (!strcmp(name, "RootFolderIcon")) { name = strtok(NULL, " =\r\n"); if (name != NULL) SetExtraIcons(name, &icon[0]); } if (!strcmp(name, "SubFolderIcon")) { name = strtok(NULL, " =\r\n"); if (name != NULL) SetExtraIcons(name, &icon[1]); } } break; } } } fclose(fp); strcpy(buf, files.name); char *ext = strrchr(buf, '.'); if (ext && *(ext + 1) && !core_stricmp(ext + 1, "ini")) { ExtraFolderData[count] =(EXFOLDERDATA*) malloc(sizeof(EXFOLDERDATA)); if (ExtraFolderData[count]) { *ext = '\0'; memset(ExtraFolderData[count], 0, sizeof(EXFOLDERDATA)); strncpy(ExtraFolderData[count]->m_szTitle, buf, 63); ExtraFolderData[count]->m_nFolderId = next_folder_id++; ExtraFolderData[count]->m_nParent = -1; ExtraFolderData[count]->m_dwFlags = F_CUSTOM; ExtraFolderData[count]->m_nIconId = icon[0] ? -icon[0] : IDI_FOLDER; ExtraFolderData[count]->m_nSubIconId = icon[1] ? -icon[1] : IDI_FOLDER; count++; } } } } } while( _findnext(hLong, &files) == 0); chdir(curdir); _findclose(hLong); return count;}
And when i complied it, i got this error:
Compiling src/osd/winui/winui_opts.cpp... ../../../../../src/osd/winui/treeview.cpp: In function 'int InitExtraFolders()': ../../../../../src/osd/winui/treeview.cpp:2248:14: warning: 'char* strncpy(char*, const char*,size_t)' output may be truncated copying 63 bytes from a string of length 255 [-Wstringop-truncation] 2248 | strncpy(ExtraFolderData[count]->m_szTitle, buf, 63); | ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Is it ok when i changed the buffer from char buf[256]; to char buf[64];
- Assistant
- User
m_szTitle is set to 64 chars in treeview.h
typedef struct { char m_szTitle[64]; // Folder Title UINT m_nFolderId; // ID int m_nParent; // Parent Folder index in treeFolders[] DWORD m_dwFlags; // Flags - Customizable and Filters int m_nIconId; // negative icon index into the ImageList, or IDI_xxx resource id int m_nSubIconId; // negative icon index into the ImageList, or IDI_xxx resource id } EXFOLDERDATA, *LPEXFOLDERDATA;
Is it ok when i only replace
strncpy(ExtraFolderData[count]->m_szTitle, buf, 63); with your suggestion snprintf(ExtraFolderData[count]->m_szTitle, sizeof(ExtraFolderData[count]->m_szTitle), "%s", buf);
- Assistant
Yes, using
snprintfas a replacement forstrncpyin this case is a suitable approach, especially sincem_szTitleis already defined as a fixed size of 64 characters in yourEXFOLDERDATAstructure. This change will help to ensure that the string is properly null-terminated and prevents any risk of buffer overflow associated with traditionalstrncpybehavior.Here's how you can implement the change:
snprintf(ExtraFolderData[count]->m_szTitle, sizeof(ExtraFolderData[count]->m_szTitle), "%s", buf); - User
Good work! Many Thanks!
- Assistant
You're welcome! I'm glad I could help. If you have any more questions or need further assistance, feel free to ask. Happy coding!