From 1587dbf71a37e118eda2ebc4ae11bfc688207d77 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 17 Feb 2016 22:53:37 -0800 Subject: [PATCH] change some sprintf() to snprintf() in lib code --- clientgui/sg_ProjectPanel.cpp | 6 +++--- clientgui/sg_TaskPanel.cpp | 8 ++++---- clientscr/ss_app.cpp | 6 +++--- lib/app_ipc.cpp | 6 +++--- lib/app_ipc.h | 2 +- lib/parse.cpp | 24 ++++++++++++------------ 6 files changed, 26 insertions(+), 26 deletions(-) diff --git a/clientgui/sg_ProjectPanel.cpp b/clientgui/sg_ProjectPanel.cpp index 602962f532..030da1b01c 100644 --- a/clientgui/sg_ProjectPanel.cpp +++ b/clientgui/sg_ProjectPanel.cpp @@ -526,12 +526,12 @@ void CSimpleProjectPanel::UpdateProjectList() { std::string CSimpleProjectPanel::GetProjectIconLoc(char* project_url) { - char urlDirectory[256]; + char proj_dir[256]; CMainDocument* pDoc = wxGetApp().GetDocument(); PROJECT* project = pDoc->state.lookup_project(project_url); if (!project) return (std::string)""; - url_to_project_dir(project->master_url, urlDirectory); - return (std::string)urlDirectory + "/stat_icon"; + url_to_project_dir(project->master_url, proj_dir, sizeof(proj_dir)); + return (std::string)proj_dir + "/stat_icon"; } diff --git a/clientgui/sg_TaskPanel.cpp b/clientgui/sg_TaskPanel.cpp index 3ac2016857..8ff4578156 100644 --- a/clientgui/sg_TaskPanel.cpp +++ b/clientgui/sg_TaskPanel.cpp @@ -885,7 +885,7 @@ wxString CSimpleTaskPanel::GetStatusString(RESULT* result) { void CSimpleTaskPanel::FindSlideShowFiles(TaskSelectionData *selData) { RESULT* state_result; - char urlDirectory[1024]; + char proj_dir[1024]; char fileName[1024]; char resolvedFileName[1024]; int j; @@ -900,9 +900,9 @@ void CSimpleTaskPanel::FindSlideShowFiles(TaskSelectionData *selData) { state_result = pDoc->state.lookup_result(selData->result->project_url, selData->result->name); } if (state_result) { - url_to_project_dir(state_result->project->master_url, urlDirectory); + url_to_project_dir(state_result->project->master_url, proj_dir, sizeof(proj_dir)); for(j=0; j<99; ++j) { - sprintf(fileName, "%s/slideshow_%s_%02d", urlDirectory, state_result->app->name, j); + sprintf(fileName, "%s/slideshow_%s_%02d", proj_dir, state_result->app->name, j); if(boinc_resolve_filename(fileName, resolvedFileName, sizeof(resolvedFileName)) == 0) { if (boinc_file_exists(resolvedFileName)) { selData->slideShowFileNames.Add(wxString(resolvedFileName,wxConvUTF8)); @@ -914,7 +914,7 @@ void CSimpleTaskPanel::FindSlideShowFiles(TaskSelectionData *selData) { if ( selData->slideShowFileNames.size() == 0 ) { for(j=0; j<99; ++j) { - sprintf(fileName, "%s/slideshow_%02d", urlDirectory, j); + sprintf(fileName, "%s/slideshow_%02d", proj_dir, j); if(boinc_resolve_filename(fileName, resolvedFileName, sizeof(resolvedFileName)) == 0) { if (boinc_file_exists(resolvedFileName)) { selData->slideShowFileNames.Add(wxString(resolvedFileName,wxConvUTF8)); diff --git a/clientscr/ss_app.cpp b/clientscr/ss_app.cpp index 13deada51c..8213ab7cc7 100644 --- a/clientscr/ss_app.cpp +++ b/clientscr/ss_app.cpp @@ -120,7 +120,7 @@ struct PROJECT_IMAGES { vector project_images; void icon_path(PROJECT* p, char* buf) { char dir[256]; - url_to_project_dir((char*)p->master_url.c_str(), dir); + url_to_project_dir((char*)p->master_url.c_str(), dir, sizeof(dir)); sprintf(buf, "%s/stat_icon", dir); } @@ -128,7 +128,7 @@ void slideshow(PROJECT* p) { char dir[256], buf[256]; int i; - url_to_project_dir((char*)p->master_url.c_str(), dir); + url_to_project_dir((char*)p->master_url.c_str(), dir, sizeof(dir)); for (i=0; i<99; i++) { sprintf(buf, "%s/slideshow_%02d", dir, i); } @@ -144,7 +144,7 @@ PROJECT_IMAGES* get_project_images(PROJECT* p) { } PROJECT_IMAGES pim; pim.url = p->master_url; - url_to_project_dir((char*)p->master_url.c_str(), dir); + url_to_project_dir((char*)p->master_url.c_str(), dir, sizeof(dir)); sprintf(path, "%s/stat_icon", dir); boinc_resolve_filename(path, filename, 256); pim.icon.load_image_file(filename); diff --git a/lib/app_ipc.cpp b/lib/app_ipc.cpp index 7889cf5d3b..2a409412de 100644 --- a/lib/app_ipc.cpp +++ b/lib/app_ipc.cpp @@ -523,16 +523,16 @@ string resolve_soft_link(const char* project_dir, const char* file) { if (!parse_str(buf, "", physical_name, sizeof(physical_name))) { return string(""); } - sprintf(buf, "../../%s/", project_dir); + snprintf(buf, sizeof(buf), "../../%s/", project_dir); if (strstr(physical_name, buf) != physical_name) { return string(""); } return string(physical_name + strlen(buf)); } -void url_to_project_dir(char* url, char* dir) { +void url_to_project_dir(char* url, char* dir, int dirsize) { char buf[256]; escape_project_url(url, buf); - sprintf(dir, "%s/%s", PROJECT_DIR, buf); + snprintf(dir, dirsize, "%s/%s", PROJECT_DIR, buf); } diff --git a/lib/app_ipc.h b/lib/app_ipc.h index f49b80d4c1..3ec4a1b09d 100644 --- a/lib/app_ipc.h +++ b/lib/app_ipc.h @@ -252,7 +252,7 @@ int parse_graphics_file(FILE* f, GRAPHICS_INFO* gi); extern int boinc_link(const char* phys_name, const char* logical_name); extern int boinc_resolve_filename_s(const char*, std::string&); extern std::string resolve_soft_link(const char* project_dir, const char* file); -extern void url_to_project_dir(char* url, char* dir); +extern void url_to_project_dir(char* url, char* dir, int dirsize); extern "C" { #endif diff --git a/lib/parse.cpp b/lib/parse.cpp index 24a983794c..4fb8c679ab 100644 --- a/lib/parse.cpp +++ b/lib/parse.cpp @@ -68,13 +68,13 @@ bool parse_bool(const char* buf, const char* tag, bool& result) { if (!strstr(buf, tag)) { return false; } - sprintf(tag2, "<%s/>", tag); - sprintf(tag3, "<%s />", tag); + snprintf(tag2, sizeof(tag2), "<%s/>", tag); + snprintf(tag3, sizeof(tag3), "<%s />", tag); if (match_tag(buf, tag2) || match_tag(buf, tag3)) { result = true; return true; } - sprintf(tag2, "<%s>", tag); + snprintf(tag2, sizeof(tag2), "<%s>", tag); if (parse_int(buf, tag2, x)) { result = (x != 0); return true; @@ -198,13 +198,13 @@ int dup_element(FILE* in, const char* tag_name, char** pp) { char buf[256], end_tag[256]; int retval; - sprintf(buf, "<%s>\n", tag_name); - sprintf(end_tag, "", tag_name); + snprintf(buf, sizeof(buf), "<%s>\n", tag_name); + snprintf(end_tag, sizeof(end_tag), "", tag_name); char* p = strdup(buf); while (fgets(buf, 256, in)) { if (strstr(buf, end_tag)) { - sprintf(buf, "\n", tag_name); + snprintf(buf, sizeof(buf), "\n", tag_name); retval = strcatdup(p, buf); if (retval) return retval; *pp = p; @@ -314,7 +314,7 @@ void extract_venue(const char* in, const char* venue_name, char* out, int len) { const char* p, *q; char* wp; char buf[256]; - sprintf(buf, "", venue_name); + snprintf(buf, sizeof(buf), "", venue_name); p = strstr(in, buf); if (p) { // prefs contain the specified venue @@ -366,7 +366,7 @@ void non_ascii_escape(const char* in, char* out, int len) { int x = (int) *in; x &= 0xff; // just in case if (x>127) { - sprintf(buf, "&#%d;", x); + snprintf(buf, sizeof(buf), "&#%d;", x); strcpy(p, buf); p += strlen(buf); } else { @@ -397,7 +397,7 @@ void xml_escape(const char* in, char* out, int len) { strcpy(p, "&"); p += 5; } else if (x>127) { - sprintf(buf, "&#%d;", x); + snprintf(buf, sizeof(buf), "&#%d;", x); strcpy(p, buf); p += strlen(buf); } else if (x<32) { @@ -405,7 +405,7 @@ void xml_escape(const char* in, char* out, int len) { case 9: case 10: case 13: - sprintf(buf, "&#%d;", x); + snprintf(buf, sizeof(buf), "&#%d;", x); strcpy(p, buf); p += strlen(buf); break; @@ -879,7 +879,7 @@ void XML_PARSER::skip_unexpected( ); } if (strchr(start_tag, '/')) return; - sprintf(end_tag, "/%s", start_tag); + snprintf(end_tag, sizeof(end_tag), "/%s", start_tag); while (1) { int c; @@ -913,7 +913,7 @@ int XML_PARSER::copy_element(string& out) { out = "<"; out += parsed_tag; out += ">"; - sprintf(end_tag, "", parsed_tag); + snprintf(end_tag, sizeof(end_tag), "", parsed_tag); int retval = element_contents(end_tag, buf, sizeof(buf)); if (retval) return retval; out += buf;