diff --git a/main/gbc/gb_str.c b/main/gbc/gb_str.c index a7a75f8ca..01ce5048f 100644 --- a/main/gbc/gb_str.c +++ b/main/gbc/gb_str.c @@ -4,7 +4,7 @@ Common string management routines - (c) 2000-2005 Benoît Minisini + (c) 2000-2005 Beno�t Minisini This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -56,7 +56,7 @@ PUBLIC char *STR_copy(const char *str) char *cpy; ALLOC(&cpy, strlen(str) + 1, "STR_copy"); - strcpy(cpy, str); + strlcpy(cpy, str, strlen(str)+1); return cpy; } diff --git a/main/gbc/gbc_archive.c b/main/gbc/gbc_archive.c index 9eba8f1f7..fc7b9a0ac 100644 --- a/main/gbc/gbc_archive.c +++ b/main/gbc/gbc_archive.c @@ -141,7 +141,7 @@ PUBLIC void ARCH_define_project(const char *project) ARCH_project_name = STR_copy(name); if (!ARCH_output) - ARCH_define_output(strcat((char *)FILE_cat(dir, ARCH_project_name, NULL), ".gambas")); + ARCH_define_output(strlcat((char *)FILE_cat(dir, ARCH_project_name, NULL), ".gambas", sizeof(file_buffer))); STR_free(name); STR_free(dir); @@ -209,8 +209,8 @@ static void compress_file_name(const char *src, int lsrc, char **dst, int *ldst) THROW("&1: not in archive!\n", tpath); } - len = sprintf(tpath2, "/%d:%s", ind, p + 1); - strcpy(tpath, tpath2); + len = snprintf(tpath2, sizeof(tpath2), "/%d:%s", ind, p + 1); + strlcpy(tpath, tpath2, sizeof(tpath)); } if (ARCH_verbose) diff --git a/main/gbc/gbc_class.c b/main/gbc/gbc_class.c index 6c6cd4f43..1bd94466d 100644 --- a/main/gbc/gbc_class.c +++ b/main/gbc/gbc_class.c @@ -172,7 +172,7 @@ CLASS_SYMBOL *CLASS_declare(CLASS *class, int index, boolean global) || (!global && !TYPE_is_null(sym->local.type))) { char name[MAX_SYMBOL_LEN + 1]; - sprintf(name, "%.*s", sym->symbol.len, sym->symbol.name); + snprintf(name, sizeof(name), "%.*s", sym->symbol.len, sym->symbol.name); THROW("'&1' already declared", name); } @@ -606,7 +606,7 @@ char *FUNCTION_get_fake_name(int func) { static char buf[6]; - sprintf(buf, "$%d", func); + snprintf(buf, sizeof(buf), "$%d", func); return buf; } diff --git a/main/gbc/gbc_compile.c b/main/gbc/gbc_compile.c index 181bae802..97219e23a 100644 --- a/main/gbc/gbc_compile.c +++ b/main/gbc/gbc_compile.c @@ -96,7 +96,7 @@ static void add_list_file(char *library) int len; path = (char *)FILE_cat(COMP_info_path, library, NULL); - strcat(path, ".list"); + strlcat(path, ".list", sizeof(file_buffer)); /*printf("Reading component list file %s\n", path);*/ @@ -105,8 +105,8 @@ static void add_list_file(char *library) if (!fi) { // Try the user component directory - path = (char *)FILE_cat(COMP_info_user_path, library, NULL); - strcat(path, ".list"); + path = (char *)FILE_cat(COMP_info_user_path, library, NULL); + strlcat(path, ".list", sizeof(file_buffer)); fi = fopen(path, "r"); } diff --git a/main/gbc/gbc_header.c b/main/gbc/gbc_header.c index 1f3b9386d..cf46a6721 100644 --- a/main/gbc/gbc_header.c +++ b/main/gbc/gbc_header.c @@ -52,7 +52,7 @@ static char *get_num_desc(int num) if (num < 4) return num_desc[num - 1]; - sprintf(desc, "%dth", num); + snprintf(desc, sizeof(desc), "%dth", num); return desc; } diff --git a/main/gbc/gbc_output.c b/main/gbc/gbc_output.c index 92e6af442..29b6514db 100644 --- a/main/gbc/gbc_output.c +++ b/main/gbc/gbc_output.c @@ -1079,11 +1079,8 @@ static void output_debug_filename(void) if (JOB->name[0] == '/') { -#ifdef OS_OPENBSD + strlcpy(path, JOB->name, sizeof(path)); -#else - strcpy(path, JOB->name); -#endif } else { @@ -1091,12 +1088,9 @@ static void output_debug_filename(void) n = strlen(path); if (path[n - 1] != '/') -#ifdef OS_OPENBSD strlcpy(&path[n], "/", sizeof(path)-n); -#else - strcpy(&path[n], "/"); -#endif - strcat(&path[n], JOB->name); + + strlcat(&path[n], JOB->name, sizeof(path)-n); } n = strlen(path); diff --git a/main/gbc/gbc_read.c b/main/gbc/gbc_read.c index fb319d970..6b9540c94 100644 --- a/main/gbc/gbc_read.c +++ b/main/gbc/gbc_read.c @@ -161,7 +161,7 @@ char *READ_get_pattern(PATTERN *pattern) if (ispunct(*str)) snprintf(COMMON_buffer, COMMON_BUF_MAX, "%s%s%s", before, str, after); else - strcpy(COMMON_buffer, str); + strlcpy(COMMON_buffer, str, COMMON_BUF_MAX); break; case RT_NUMBER: @@ -176,20 +176,20 @@ char *READ_get_pattern(PATTERN *pattern) break; case RT_NEWLINE: - strcpy(COMMON_buffer, "end of line"); + strlcpy(COMMON_buffer, "end of line", COMMON_BUF_MAX); break; case RT_END: - strcpy(COMMON_buffer, "end of file"); + strlcpy(COMMON_buffer, "end of file", COMMON_BUF_MAX); break; case RT_SUBR: //snprintf(COMMON_buffer, COMMON_BUF_MAX, "%s%s%s", bafore, COMP_subr_info[index].name, after); - strcpy(COMMON_buffer, COMP_subr_info[index].name); + strlcpy(COMMON_buffer, COMP_subr_info[index].name, COMMON_BUF_MAX); break; default: - sprintf(COMMON_buffer, "%s?%p?%s", before, (void *)*pattern, after); + snprintf(COMMON_buffer, COMMON_BUF_MAX, "%s?%p?%s", before, (void *)*pattern, after); } return COMMON_buffer; diff --git a/main/gbc/gbc_type.c b/main/gbc/gbc_type.c index 09f627da3..824759ff8 100644 --- a/main/gbc/gbc_type.c +++ b/main/gbc/gbc_type.c @@ -152,12 +152,12 @@ char *TYPE_get_desc(TYPE type) if (id == T_ARRAY) { - strcpy(buf, TYPE_name[JOB->class->array[value].type.t.id]); - strcat(buf, "[]"); + strlcpy(buf, TYPE_name[JOB->class->array[value].type.t.id], sizeof(buf)); + strlcat(buf, "[]", sizeof(buf)); } else { - strcpy(buf, TYPE_name[id]); + strlcpy(buf, TYPE_name[id], sizeof(buf)); } return buf; diff --git a/main/gbc/gbi.c b/main/gbc/gbi.c index f77e88a1c..8f3ce4df9 100644 --- a/main/gbc/gbi.c +++ b/main/gbc/gbi.c @@ -160,13 +160,8 @@ static void init(void) strncpy(_root, FILE_get_dir(FILE_get_dir(path)), MAX_PATH); } -#ifdef OS_OPENBSD strlcpy(_lib_path, FILE_cat(_root, "lib/gambas" GAMBAS_VERSION_STRING, NULL), sizeof(_lib_path)); strlcpy(_info_path, FILE_cat(_root, "share/gambas" GAMBAS_VERSION_STRING "/info", NULL), sizeof(_info_path)); -#else - strcpy(_lib_path, FILE_cat(_root, "lib/gambas" GAMBAS_VERSION_STRING, NULL)); - strcpy(_info_path, FILE_cat(_root, "share/gambas" GAMBAS_VERSION_STRING "/info", NULL)); -#endif if (lt_dlinit()) error(TRUE, "Cannot initialize plug-in management: %s", lt_dlerror()); @@ -615,7 +610,7 @@ static void preload(char **argv, char *lib) if (_nopreload || getenv("GB_PRELOAD") || !lib || !*lib) return; - sprintf(buf, "LD_PRELOAD=%s", lib); + snprintf(buf, sizeof(buf), "LD_PRELOAD=%s", lib); putenv(buf); putenv("GB_PRELOAD=1"); @@ -634,9 +629,9 @@ static void analyze(const char *comp, bool include) name = STR_copy(comp); - sprintf(_buffer, LIB_PATTERN, _lib_path, name); + snprintf(_buffer, sizeof(_buffer), LIB_PATTERN, _lib_path, name); native = (access(_buffer, F_OK) == 0); - sprintf(_buffer, ARCH_PATTERN, _lib_path, name); + snprintf(_buffer, sizeof(_buffer), ARCH_PATTERN, _lib_path, name); gambas = (access(_buffer, F_OK) == 0); if (!native && !gambas) @@ -665,7 +660,7 @@ static void analyze(const char *comp, bool include) if (native) { - sprintf(_buffer, LIB_PATTERN, _lib_path, name); + snprintf(_buffer, sizeof(_buffer), LIB_PATTERN, _lib_path, name); if (analyze_native_component(_buffer)) ok = FALSE; @@ -673,7 +668,7 @@ static void analyze(const char *comp, bool include) if (gambas) { - sprintf(_buffer, ARCH_PATTERN, _lib_path, name); + snprintf(_buffer, sizeof(_buffer), ARCH_PATTERN, _lib_path, name); if (analyze_gambas_component(_buffer)) if (!native) diff --git a/main/gbx/gb_error.c b/main/gbx/gb_error.c index a07c7c751..4f7704afa 100644 --- a/main/gbx/gb_error.c +++ b/main/gbx/gb_error.c @@ -468,11 +468,7 @@ PUBLIC void ERROR_save(ERROR_INFO *save) save->pc = ERROR_info.pc; save->backtrace = ERROR_info.backtrace; ERROR_info.backtrace = NULL; -#ifdef OS_OPENBSD strlcpy(save->msg, ERROR_info.msg, sizeof(ERROR_info.msg)); -#else - strcpy(save->msg, ERROR_info.msg); -#endif } PUBLIC void ERROR_restore(ERROR_INFO *save) @@ -485,11 +481,7 @@ PUBLIC void ERROR_restore(ERROR_INFO *save) ERROR_info.pc = save->pc; ERROR_info.backtrace = save->backtrace; save->backtrace = NULL; -#ifdef OS_OPENBSD strlcpy(ERROR_info.msg, save->msg, sizeof(ERROR_info.msg)); -#else - strcpy(ERROR_info.msg, save->msg); -#endif } diff --git a/main/gbx/gbx_api.c b/main/gbx/gbx_api.c index 5313345af..60b055c7a 100644 --- a/main/gbx/gbx_api.c +++ b/main/gbx/gbx_api.c @@ -717,13 +717,8 @@ PUBLIC void *GB_GetClassInterface(void *_class, const char *_name) CLASS_load(class); -#ifdef OS_OPENBSD strlcpy(name, "_@", len+4); strlcat(name, _name, len+4); -#else - strcpy(name, "_@"); - strcat(name, _name); -#endif index = CLASS_find_symbol(class, name); if (index == NO_SYMBOL) @@ -1591,11 +1586,7 @@ PUBLIC char *GB_RealFileName(const char *name, int len) temp = FILE_make_temp(NULL, NULL); STRING_new_temp(&real, NULL, strlen(temp) + strlen(path) + strlen("/data/")); -#ifdef OS_OPENBSD snprintf(real, strlen(temp) + strlen(path) + strlen("/data/"), "%s/data/%s", temp, path); -#else - sprintf(real, "%s/data/%s", temp, path); -#endif if (!FILE_exist(real)) { diff --git a/main/gbx/gbx_archive.c b/main/gbx/gbx_archive.c index 7803af77e..cc0f89443 100644 --- a/main/gbx/gbx_archive.c +++ b/main/gbx/gbx_archive.c @@ -155,9 +155,9 @@ PUBLIC void ARCHIVE_load(ARCHIVE *arch) { char *path = FILE_buffer(); - sprintf(path, ARCH_PATTERN, COMPONENT_path, arch->name); + snprintf(path, FILE_buffer_maxsize(), ARCH_PATTERN, COMPONENT_path, arch->name); if (!FILE_exist(path)) - sprintf(path, ARCH_PATTERN, COMPONENT_user_path, arch->name); + snprintf(path, FILE_buffer_maxsize(), ARCH_PATTERN, COMPONENT_user_path, arch->name); load_archive(arch, path); } diff --git a/main/gbx/gbx_c_application.c b/main/gbx/gbx_c_application.c index a0874afaa..a320c0b13 100644 --- a/main/gbx/gbx_c_application.c +++ b/main/gbx/gbx_c_application.c @@ -292,12 +292,8 @@ static void init_again(int old_pid) char old[MAX_PATH]; FILE_remove_temp_file(); -#ifdef OS_OPENBSD - snprintf(old, sizeof(old),FILE_TEMP_DIR, getuid(), old_pid); -#else - sprintf(old, FILE_TEMP_DIR, getuid(), old_pid); -#endif - rename(old, FILE_make_temp(NULL, NULL)); + snprintf(old, sizeof(old),FILE_TEMP_DIR, getuid(), old_pid); + rename(old, FILE_make_temp(NULL, NULL)); } BEGIN_PROPERTY(CAPPLICATION_daemon) diff --git a/main/gbx/gbx_c_file.c b/main/gbx/gbx_c_file.c index 29f76a5fb..c04bc9030 100644 --- a/main/gbx/gbx_c_file.c +++ b/main/gbx/gbx_c_file.c @@ -259,7 +259,7 @@ static char *get_file_user(CFILE *_object) pwd = getpwuid(uid); if (!pwd) { - sprintf(_buffer, "%d", uid); + snprintf(_buffer, sizeof(_buffer), "%d", uid); return _buffer; } else @@ -286,7 +286,7 @@ static char *get_file_group(CFILE *_object) grp = getgrgid(gid); if (!grp) { - sprintf(_buffer, "%d", gid); + snprintf(_buffer, sizeof(_buffer), "%d", gid); return _buffer; } else diff --git a/main/gbx/gbx_c_quote.c b/main/gbx/gbx_c_quote.c index e54972db5..7ff333e54 100644 --- a/main/gbx/gbx_c_quote.c +++ b/main/gbx/gbx_c_quote.c @@ -59,7 +59,7 @@ BEGIN_METHOD(CQUOTE_call, GB_STRING str) STRING_add(&result, "\\\\", 2); else if (c < ' ' || c > 126) { - sprintf(buf, "\\x%02X", c); + snprintf(buf, sizeof(buf), "\\x%02X", c); STRING_add(&result, buf, 4); } else @@ -102,7 +102,7 @@ BEGIN_METHOD(CQUOTE_shell, GB_STRING str) STRING_add(&result, "$'\\t'", 5); else if (c < ' ') //|| (c > 126 && !LOCAL_is_UTF8)) { - sprintf(buf, "$'\\x%02X'", c); + snprintf(buf, sizeof(buf), "$'\\x%02X'", c); STRING_add(&result, buf, 7); } else if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || index(".-/_~", c) || c > 126) diff --git a/main/gbx/gbx_class.c b/main/gbx/gbx_class.c index 682f74782..73cc80f8d 100644 --- a/main/gbx/gbx_class.c +++ b/main/gbx/gbx_class.c @@ -363,11 +363,7 @@ CLASS *CLASS_find(const char *name) _classes = csym->class; ALLOC(&csym->class->name, len + 1, "CLASS_find"); - #ifdef OS_OPENBSD strlcpy((char *)csym->class->name, name, len + 1); - #else - strcpy((char *)csym->class->name, name); - #endif csym->sym.name = csym->class->name; @@ -722,11 +718,7 @@ CLASS *CLASS_replace_global(const char *name) len = strlen(name); ALLOC(&new_name, len + 2, "CLASS_replace_global"); -#ifdef OS_OPENBSD snprintf(new_name, len+2, ">%s", name); -#else - sprintf(new_name, ">%s", name); -#endif new_class = CLASS_replace_global(new_name); FREE(&new_name, "CLASS_replace_global"); diff --git a/main/gbx/gbx_class_load.c b/main/gbx/gbx_class_load.c index 57daf9c12..c8c52f93b 100644 --- a/main/gbx/gbx_class_load.c +++ b/main/gbx/gbx_class_load.c @@ -330,11 +330,7 @@ PUBLIC void CLASS_load_without_init(CLASS *class) char name[len + 9]; char *p; - #ifdef OS_OPENBSD strlcpy(name, ".gambas/", len+9); - #else - strcpy(name, ".gambas/"); - #endif p = &name[8]; for (i = 0; i < len; i++) diff --git a/main/gbx/gbx_component.c b/main/gbx/gbx_component.c index 6e9090c5f..10c28993a 100644 --- a/main/gbx/gbx_component.c +++ b/main/gbx/gbx_component.c @@ -157,7 +157,7 @@ PUBLIC COMPONENT *COMPONENT_create(const char *name) // System wide component, located in /usr/local/lib/gambas2 (by default) path = FILE_buffer(); - sprintf(path, LIB_PATTERN, COMPONENT_path, name); + snprintf(path, FILE_buffer_maxsize(), LIB_PATTERN, COMPONENT_path, name); if (FILE_exist(path)) comp->library = LIBRARY_create(name); @@ -165,7 +165,7 @@ PUBLIC COMPONENT *COMPONENT_create(const char *name) if (can_archive) { path = FILE_buffer(); - sprintf(path, ARCH_PATTERN, COMPONENT_path, name); + snprintf(path, FILE_buffer_maxsize(), ARCH_PATTERN, COMPONENT_path, name); if (FILE_exist(path)) comp->archive = ARCHIVE_create(name); @@ -177,7 +177,7 @@ PUBLIC COMPONENT *COMPONENT_create(const char *name) // User specific component, located in ~/.local/lib/gambas2 path = FILE_buffer(); - sprintf(path, LIB_PATTERN, COMPONENT_user_path, name); + snprintf(path, FILE_buffer_maxsize(), LIB_PATTERN, COMPONENT_user_path, name); if (FILE_exist(path)) comp->library = LIBRARY_create(name); @@ -185,7 +185,7 @@ PUBLIC COMPONENT *COMPONENT_create(const char *name) if (can_archive) { path = FILE_buffer(); - sprintf(path, ARCH_PATTERN, COMPONENT_user_path, name); + snprintf(path, FILE_buffer_maxsize(), ARCH_PATTERN, COMPONENT_user_path, name); if (FILE_exist(path)) comp->archive = ARCHIVE_create(name); diff --git a/main/gbx/gbx_debug.c b/main/gbx/gbx_debug.c index a9c02ad64..092c5adb4 100644 --- a/main/gbx/gbx_debug.c +++ b/main/gbx/gbx_debug.c @@ -81,18 +81,14 @@ PUBLIC const char *DEBUG_get_position(CLASS *cp, FUNCTION *fp, PCODE *pc) calc_line_from_position(cp, fp, pc, &line); #if DEBUG_MEMORY - sprintf(buffer, "%s.%s.%d", + snprintf(buffer, sizeof(buffer), "%s.%s.%d", cp ? cp->name : "?", (fp && fp->debug) ? fp->debug->name : "?", line); return buffer; #else -#ifdef OS_OPENBSD snprintf(COMMON_buffer, COMMON_BUF_MAX, "%.64s.%.64s.%d", -#else - sprintf(COMMON_buffer, "%.64s.%.64s.%d", -#endif cp ? cp->name : "?", (fp && fp->debug) ? fp->debug->name : "?", line); diff --git a/main/gbx/gbx_extern.c b/main/gbx/gbx_extern.c index 46762d7c4..a8357b74c 100644 --- a/main/gbx/gbx_extern.c +++ b/main/gbx/gbx_extern.c @@ -69,9 +69,9 @@ static lt_dlhandle get_library(char *name) p = strrchr(name, ':'); if (!p) - sprintf(COMMON_buffer, "%s." SHARED_LIBRARY_EXT, name); + snprintf(COMMON_buffer, COMMON_BUF_MAX, "%s." SHARED_LIBRARY_EXT, name); else - sprintf(COMMON_buffer, "%.*s." SHARED_LIBRARY_EXT ".%s", p - name, name, p + 1); + snprintf(COMMON_buffer, COMMON_BUF_MAX, "%.*s." SHARED_LIBRARY_EXT ".%s", p - name, name, p + 1); name = COMMON_buffer; diff --git a/main/gbx/gbx_library.c b/main/gbx/gbx_library.c index 18f04a284..ac1305ec6 100644 --- a/main/gbx/gbx_library.c +++ b/main/gbx/gbx_library.c @@ -87,11 +87,7 @@ static void *get_symbol(LIBRARY *lib, const char *symbol, bool err) sym = lt_dlsym(lib->handle, symbol); if (sym == NULL && err) { -#ifdef OS_OPENBSD strlcpy(COMMON_buffer, lt_dlerror(), COMMON_BUF_MAX); -#else - strcpy(COMMON_buffer, lt_dlerror()); -#endif lt_dlclose(lib->handle); lib->handle = NULL; THROW(E_LIBRARY, lib->name, COMMON_buffer); @@ -153,18 +149,10 @@ static void add_preload(char **env, const char *lib) { org = getenv("LD_PRELOAD"); if (org && *org) -#ifdef OS_OPENBSD *env += snprintf(*env, COMMON_BUF_MAX, "%s ", org); -#else - *env += sprintf(*env, "%s ", org); -#endif - } + } -#ifdef OS_OPENBSD - *env += snprintf(*env, &COMMON_buffer[COMMON_BUF_MAX] - *env, "%s ", lib); -#else - *env += sprintf(*env, "%s ", lib); -#endif + *env += snprintf(*env, (&COMMON_buffer[COMMON_BUF_MAX] - *env), "%s ", lib); } @@ -208,11 +196,7 @@ PUBLIC void LIBRARY_preload(const char *file, char **argv) if (*file == '/') { -#ifdef OS_OPENBSD strlcpy(dir, file, sizeof(dir)); -#else - strcpy(dir, file); -#endif } else { @@ -229,11 +213,7 @@ PUBLIC void LIBRARY_preload(const char *file, char **argv) if (path == NULL) goto _PANIC; -#ifdef OS_OPENBSD strlcpy(dir, path, sizeof(dir)); -#else - strcpy(dir, path); -#endif } file = FILE_cat(dir, ".project", NULL); @@ -369,11 +349,7 @@ PUBLIC void LIBRARY_get_interface(LIBRARY *lib, long version, void *iface) symbol[i] = c; } -#ifdef OS_OPENBSD snprintf(&symbol[len], sizeof(symbol)-len, "_%ld", version); -#else - sprintf(&symbol[len], "_%ld", version); -#endif copy_interface((long *)get_symbol(lib, symbol, TRUE), (long *)iface); } @@ -439,17 +415,9 @@ PUBLIC void LIBRARY_load(LIBRARY *lib) return; path = FILE_buffer(); -#ifdef OS_OPENBSD - snprintf(path, PATH_MAX, LIB_PATTERN, COMPONENT_path, lib->name); -#else - sprintf(path, LIB_PATTERN, COMPONENT_path, lib->name); -#endif + snprintf(path, FILE_buffer_maxsize(), LIB_PATTERN, COMPONENT_path, lib->name); if (!FILE_exist(path)) -#ifdef OS_OPENBSD - snprintf(path, PATH_MAX, LIB_PATTERN, COMPONENT_user_path, lib->name); -#else - sprintf(path, LIB_PATTERN, COMPONENT_user_path, lib->name); -#endif + snprintf(path, FILE_buffer_maxsize(), LIB_PATTERN, COMPONENT_user_path, lib->name); #ifndef DONT_USE_LTDL /* no more available in libltld ? diff --git a/main/gbx/gbx_local.c b/main/gbx/gbx_local.c index b37d43fbf..01a782abe 100644 --- a/main/gbx/gbx_local.c +++ b/main/gbx/gbx_local.c @@ -147,7 +147,7 @@ static void stradd_sep(char *dst, const char *src, const char *sep) { if (*dst) strcat(dst, sep); - strcat(dst, src); + strcat(dst, src); } @@ -884,7 +884,7 @@ _FORMAT: if (exposant) number_exp = number != 0.0; - ndigit = sprintf(buf, "%.*f", MinMax(after + number_exp, 0, DBL_DIG), number_mant); + ndigit = snprintf(buf, sizeof(buf), "%.*f", MinMax(after + number_exp, 0, DBL_DIG), number_mant); // should return "0[.]...", or "1[.]..." if the number is rounded up. @@ -999,7 +999,7 @@ _EXPOSANT: if (exposant != 0) // && number != 0.0) { put_char(exposant); - n = sprintf(buf, "%+.*d", exp_zero, number_real_exp - 1); + n = snprintf(buf, sizeof(buf), "%+.*d", exp_zero, number_real_exp - 1); add_string(buf, n, NULL); } @@ -1066,7 +1066,7 @@ static void add_date_token(DATE_SERIAL *date, char *token, int count) if (count <= 2) { - n = sprintf(buf, (count == 1 ? "%d" : "%02d"), date->day); + n = snprintf(buf, sizeof(buf), (count == 1 ? "%d" : "%02d"), date->day); add_string(buf, n, NULL); } else if (count >= 3) @@ -1081,7 +1081,7 @@ static void add_date_token(DATE_SERIAL *date, char *token, int count) if (count <= 2) { - n = sprintf(buf, (count == 1 ? "%d" : "%02d"), date->month); + n = snprintf(buf, sizeof(buf), (count == 1 ? "%d" : "%02d"), date->month); add_string(buf, n, NULL); } else if (count >= 3) @@ -1095,9 +1095,9 @@ static void add_date_token(DATE_SERIAL *date, char *token, int count) case 'y': if (count <= 2 && date->year >= 1939 && date->year <= 2038) - n = sprintf(buf, "%02d", date->year - (date->year >= 2000 ? 2000 : 1900)); + n = snprintf(buf, sizeof(buf), "%02d", date->year - (date->year >= 2000 ? 2000 : 1900)); else - n = sprintf(buf, "%d", date->year); + n = snprintf(buf, sizeof(buf), "%d", date->year); add_string(buf, n, NULL); @@ -1107,7 +1107,7 @@ static void add_date_token(DATE_SERIAL *date, char *token, int count) case 'n': case 's': - n = sprintf(buf, (count == 1) ? "%d" : "%02d", + n = snprintf(buf, sizeof(buf), (count == 1) ? "%d" : "%02d", (*token == 'h') ? date->hour : ((*token == 'n') ? date->min : date->sec)); add_string(buf, n, NULL); @@ -1118,7 +1118,7 @@ static void add_date_token(DATE_SERIAL *date, char *token, int count) if (date->msec || count == 2) { - n = sprintf(buf, ".%03d", date->msec); + n = snprintf(buf, sizeof(buf), ".%03d", date->msec); if (count == 1) { while (buf[n - 1] == '0') @@ -1396,7 +1396,7 @@ PUBLIC void LOCAL_load_translation(ARCHIVE *arch) mkdir(dst, S_IRWXU); dst = FILE_cat(dst, domain, NULL); - strcat((char *)dst, ".mo"); + strlcat((char *)dst, ".mo", sizeof(file_buffer)); unlink(dst); diff --git a/main/gbx/gbx_print.c b/main/gbx/gbx_print.c index 86709044b..9e966c07e 100644 --- a/main/gbx/gbx_print.c +++ b/main/gbx/gbx_print.c @@ -83,22 +83,14 @@ __BYTE: __SHORT: __INTEGER: -#ifdef OS_OPENBSD *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "%d", value->_integer.value); -#else - *len = sprintf(COMMON_buffer, "%d", value->_integer.value); -#endif *addr = COMMON_buffer; return; __LONG: -#ifdef OS_OPENBSD *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "%lld", value->_long.value); -#else - *len = sprintf(COMMON_buffer, "%lld", value->_long.value); -#endif *addr = COMMON_buffer; return; @@ -131,7 +123,7 @@ __STRING: { if (i > 128) { - strcat(d, "..."); + strlcat(d, "...", (COMMON_BUF_MAX - *len)); *len += 3; break; } @@ -149,11 +141,7 @@ __STRING: else if (c == 9) *d++ = 't'; else -#ifdef OS_OPENBSD - d += snprintf(d, &COMMON_buffer[COMMON_BUF_MAX]-d, "x%02X", c); -#else - d += sprintf(d, "x%02X", c); -#endif + d += snprintf(d, (&COMMON_buffer[COMMON_BUF_MAX] - d), "x%02X", c); } else if (c == '\"') { @@ -181,7 +169,7 @@ __OBJECT: //*more = !CLASS_is_native(OBJECT_class(value->_object.object)); - *len = sprintf(COMMON_buffer, "(%s %p)", OBJECT_class(value->_object.object)->name, value->_object.object); + *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "(%s %p)", OBJECT_class(value->_object.object)->name, value->_object.object); *addr = COMMON_buffer; return; @@ -204,14 +192,14 @@ __CLASS: CLASS *class = value->_class.class; //*more = (!CLASS_is_native(class) && class->load->n_stat > 0); - *len = sprintf(COMMON_buffer, "%s %p", class->name, class); + *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "%s %p", class->name, class); *addr = COMMON_buffer; return; } __ARRAY: - *len = sprintf(COMMON_buffer, "(ARRAY %p)", value->_array.addr); + *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "(ARRAY %p)", value->_array.addr); *addr = COMMON_buffer; return; diff --git a/main/gbx/gbx_project.c b/main/gbx/gbx_project.c index a99a94a6f..2d419b7eb 100644 --- a/main/gbx/gbx_project.c +++ b/main/gbx/gbx_project.c @@ -71,11 +71,7 @@ static void raise_error(const char *msg) { char line[16]; -#ifdef OS_OPENBSD snprintf(line, sizeof(line), "%d", project_line); -#else - sprintf(line, "%d", project_line); -#endif THROW(E_PROJECT, line, msg); } diff --git a/main/gbx/gbx_type.c b/main/gbx/gbx_type.c index cb8772844..da3a4127e 100644 --- a/main/gbx/gbx_type.c +++ b/main/gbx/gbx_type.c @@ -295,11 +295,7 @@ PUBLIC TYPE TYPE_from_string(const char **ptype) if (*start == '*') { - #ifdef OS_OPENBSD strlcpy(COMMON_buffer, TYPE_joker, COMMON_BUF_MAX); - #else - strcpy(COMMON_buffer, TYPE_joker); - #endif start++; if (type > start) strncat(COMMON_buffer, start, type - start); diff --git a/main/gbx/gbx_value.c b/main/gbx/gbx_value.c index 5e35585d2..e3be1623d 100644 --- a/main/gbx/gbx_value.c +++ b/main/gbx/gbx_value.c @@ -311,22 +311,14 @@ __c2s: __h2s: __i2s: -#ifdef OS_OPENBSD len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "%d", value->_integer.value); -#else - len = sprintf(COMMON_buffer, "%d", value->_integer.value); -#endif STRING_new_temp_value(value, COMMON_buffer, len); BORROW(value); return; __l2s: -#ifdef OS_OPENBSD len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "%lld", value->_long.value); -#else - len = sprintf(COMMON_buffer, "%lld", value->_long.value); -#endif STRING_new_temp_value(value, COMMON_buffer, len); BORROW(value); return; @@ -1004,22 +996,14 @@ __BYTE: __SHORT: __INTEGER: -#ifdef OS_OPENBSD *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "%d", value->_integer.value); -#else - *len = sprintf(COMMON_buffer, "%d", value->_integer.value); -#endif *addr = COMMON_buffer; return; __LONG: -#ifdef OS_OPENBSD *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "%lld", value->_long.value); -#else - *len = sprintf(COMMON_buffer, "%lld", value->_long.value); -#endif *addr = COMMON_buffer; return; @@ -1046,11 +1030,7 @@ __OBJECT: if (VALUE_is_null(value)) goto __NULL; -#ifdef OS_OPENBSD *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "(%s %p)", OBJECT_class(value->_object.object)->name, value->_object.object); -#else - *len = sprintf(COMMON_buffer, "(%s %p)", OBJECT_class(value->_object.object)->name, value->_object.object); -#endif *addr = COMMON_buffer; return; @@ -1065,22 +1045,14 @@ __VOID: __CLASS: -#ifdef OS_OPENBSD *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "(Class %s)", value->_class.class->name); -#else - *len = sprintf(COMMON_buffer, "(Class %s)", value->_class.class->name); -#endif *addr = COMMON_buffer; return; __ARRAY: __FUNCTION: -#ifdef OS_OPENBSD *len = snprintf(COMMON_buffer, COMMON_BUF_MAX, "(%s ?)", TYPE_get_name(value->type)); -#else - *len = sprintf(COMMON_buffer, "(%s ?)", TYPE_get_name(value->type)); -#endif *addr = COMMON_buffer; /*THROW(E_TYPE, TYPE_get_name(T_STRING), TYPE_get_name(value->type));*/ diff --git a/main/lib/compress/main.c b/main/lib/compress/main.c index f1ff9c2cd..a5828b5fe 100644 --- a/main/lib/compress/main.c +++ b/main/lib/compress/main.c @@ -60,7 +60,8 @@ static void COMPRESS_Register(COMPRESS_DRIVER *driver) COMPRESS_DRIVER *COMPRESS_GetDriver(char *type) { int i; - char comp[strlen(type) + 14]; + int comp_size = strlen(type) + 14; + char comp[comp_size]; if (!type) { @@ -68,8 +69,8 @@ COMPRESS_DRIVER *COMPRESS_GetDriver(char *type) return NULL; } - strcpy(comp, "gb.compress."); - strcat(comp, type); + strlcpy(comp, "gb.compress.", comp_size); + strlcat(comp, type, comp_size); if (GB.LoadComponent(comp)) { diff --git a/main/lib/db/CConnection.c b/main/lib/db/CConnection.c index b1ca12251..57e5b7cf5 100644 --- a/main/lib/db/CConnection.c +++ b/main/lib/db/CConnection.c @@ -415,7 +415,7 @@ static char *make_query(CCONNECTION *_object, char *pattern, int len, int narg, if (!keyword) keyword = "LIMIT"; - sprintf(buffer, "%s %d", keyword, THIS->limit); + snprintf(buffer, sizeof(buffer), "%s %d", keyword, THIS->limit); _make_query_buffer = buffer; _make_query_original = &query[7]; diff --git a/main/lib/db/main.c b/main/lib/db/main.c index 89af573b4..3f84312f7 100644 --- a/main/lib/db/main.c +++ b/main/lib/db/main.c @@ -160,7 +160,8 @@ void DB_TryAnother(char *driver) static DB_DRIVER *DB_GetDriver(char *type) { int i; - char comp[type ? strlen(type) + 8 : 1]; + int comp_size = (type ? strlen(type) + 8 : 1); + char comp[comp_size]; if (!type) { @@ -168,8 +169,8 @@ static DB_DRIVER *DB_GetDriver(char *type) return NULL; } - strcpy(comp, "gb.db."); - strcat(comp, type); + strlcpy(comp, "gb.db.", comp_size); + strlcat(comp, type, comp_size); GB.LoadComponent(comp); GB.Error(NULL); // reset the error flag; @@ -247,13 +248,13 @@ void DB_Format(DB_DRIVER *driver, GB_VALUE *arg, DB_FORMAT_CALLBACK add) case GB_T_SHORT: case GB_T_INTEGER: - l = sprintf(buffer, "%d", VALUE((GB_INTEGER *)arg)); + l = snprintf(buffer, sizeof(buffer), "%d", VALUE((GB_INTEGER *)arg)); add(buffer, l); return; case GB_T_LONG: - l = sprintf(buffer, "%lld", VALUE((GB_LONG *)arg)); + l = snprintf(buffer, sizeof(buffer), "%lld", VALUE((GB_LONG *)arg)); add(buffer, l); return; diff --git a/main/lib/debug/debug.c b/main/lib/debug/debug.c index c692d767d..74e8baac2 100644 --- a/main/lib/debug/debug.c +++ b/main/lib/debug/debug.c @@ -171,10 +171,10 @@ PUBLIC DEBUG_INFO *DEBUG_init(GB_DEBUG_INTERFACE *debug, bool fifo) if (_fifo) { - sprintf(path, "/tmp/gambas.%d/%d.out", getuid(), getppid()); + snprintf(path, sizeof(path), "/tmp/gambas.%d/%d.out", getuid(), getppid()); _fdr = open(path, O_RDONLY); fcntl(_fdr, F_SETFD, FD_CLOEXEC); - sprintf(path, "/tmp/gambas.%d/%d.in", getuid(), getppid()); + snprintf(path, sizeof(path), "/tmp/gambas.%d/%d.in", getuid(), getppid()); _fdw = open(path, O_WRONLY); fcntl(_fdw, F_SETFD, FD_CLOEXEC); @@ -885,7 +885,7 @@ PUBLIC const char *DEBUG_get_position(CLASS *cp, FUNCTION *fp, PCODE *pc) if (fp != NULL && fp->debug) calc_line_from_position(cp, fp, pc, &line); - sprintf(DEBUG_buffer, "%.64s.%.64s.%d", + snprintf(DEBUG_buffer, sizeof(DEBUG_buffer), "%.64s.%.64s.%d", cp ? cp->name : "?", (fp && fp->debug) ? fp->debug->name : "?", line); diff --git a/main/lib/eval/eval_read.c b/main/lib/eval/eval_read.c index 8eec16a91..a4de62559 100644 --- a/main/lib/eval/eval_read.c +++ b/main/lib/eval/eval_read.c @@ -109,7 +109,7 @@ PUBLIC char *READ_get_pattern(PATTERN *pattern) if (ispunct(*str)) snprintf(_buffer, BUF_MAX, "%s%s%s", before, str, after); else - strcpy(_buffer, str); + strlcpy(_buffer, str, sizeof(_buffer)); break; case RT_NUMBER: @@ -125,16 +125,16 @@ PUBLIC char *READ_get_pattern(PATTERN *pattern) case RT_NEWLINE: case RT_END: - strcpy(_buffer, "end of expression"); + strlcpy(_buffer, "end of expression", sizeof(_buffer)); break; case RT_SUBR: //snprintf(COMMON_buffer, COMMON_BUF_MAX, "%s%s%s", bafore, COMP_subr_info[index].name, after); - strcpy(_buffer, COMP_subr_info[index].name); + strlcpy(_buffer, COMP_subr_info[index].name, sizeof(_buffer)); break; default: - sprintf(_buffer, "%s?%p?%s", before, (void *)*pattern, after); + snprintf(_buffer, sizeof(_buffer), "%s?%p?%s", before, (void *)*pattern, after); } return _buffer; diff --git a/main/share/gb_arch_temp.h b/main/share/gb_arch_temp.h index 8bc25d42d..99bf64213 100644 --- a/main/share/gb_arch_temp.h +++ b/main/share/gb_arch_temp.h @@ -252,19 +252,14 @@ PUBLIC bool ARCH_find(ARCH *arch, const char *path, int len_path, ARCH_FIND *fin p = index(tpath + 1, '/'); if (!p) break; - - SYMBOL_find(arch->symbol, arch->header.n_symbol, sizeof(ARCH_SYMBOL), TF_NORMAL, tpath, p - tpath, 0, &ind); + + SYMBOL_find(arch->symbol, arch->header.n_symbol, sizeof(ARCH_SYMBOL), TF_NORMAL, tpath, p - tpath, 0, &ind); if (ind == NO_SYMBOL) break; - - sym = &arch->symbol[ind]; -#ifdef OS_OPENBSD + + sym = &arch->symbol[ind]; len_tpath = snprintf(tpath2, sizeof(tpath2),"/%d:%s", ind, p + 1); strlcpy(tpath, tpath2, sizeof(tpath)); -#else - len_tpath = sprintf(tpath2, "/%d:%s", ind, p + 1); - strcpy(tpath, tpath2); -#endif } SYMBOL_find(arch->symbol, arch->header.n_symbol, sizeof(ARCH_SYMBOL), TF_NORMAL, tpath, len_tpath, 0, &ind); diff --git a/main/share/gb_file_share.h b/main/share/gb_file_share.h index 1a0d47d4f..785a9c327 100644 --- a/main/share/gb_file_share.h +++ b/main/share/gb_file_share.h @@ -79,6 +79,7 @@ typedef PUBLIC const char *FILE_cat(const char *path, ...); PUBLIC char *FILE_buffer(void); PUBLIC int FILE_buffer_length(void); +PUBLIC int FILE_buffer_maxsize(void); PUBLIC const char *FILE_get_dir(const char *path); PUBLIC const char *FILE_get_name(const char *path); PUBLIC const char *FILE_get_ext(const char *path); diff --git a/main/share/gb_file_temp.h b/main/share/gb_file_temp.h index 7d8f58241..85c609ada 100644 --- a/main/share/gb_file_temp.h +++ b/main/share/gb_file_temp.h @@ -131,27 +131,15 @@ PUBLIC char *FILE_make_temp(int *len, char *pattern) if (len) { if (pattern) -#ifdef OS_OPENBSD *len = snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_PATTERN, getuid(), getpid(), pattern); -#else - *len = sprintf(file_buffer, FILE_TEMP_PATTERN, getuid(), getpid(), pattern); -#endif else { count++; -#ifdef OS_OPENBSD *len = snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_FILE, getuid(), getpid(), count); -#else - *len = sprintf(file_buffer, FILE_TEMP_FILE, getuid(), getpid(), count); -#endif } } else -#ifdef OS_OPENBSD snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, getuid(), getpid()); -#else - sprintf(file_buffer, FILE_TEMP_DIR, getuid(), getpid()); -#endif return file_buffer; } @@ -180,17 +168,9 @@ PUBLIC void FILE_init(void) { FILE_remove_temp_file(); -#ifdef OS_OPENBSD snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_PREFIX, getuid()); -#else - sprintf(file_buffer, FILE_TEMP_PREFIX, getuid()); -#endif mkdir(file_buffer, S_IRWXU); -#ifdef OS_OPENBSD snprintf(file_buffer, sizeof(file_buffer), FILE_TEMP_DIR, getuid(), getpid()); -#else - sprintf(file_buffer, FILE_TEMP_DIR, getuid(), getpid()); -#endif mkdir(file_buffer, S_IRWXU); } @@ -288,6 +268,10 @@ PUBLIC int FILE_buffer_length(void) return file_buffer_length; } +PUBLIC int FILE_buffer_maxsize(void) +{ + return sizeof(file_buffer); +} PUBLIC const char *FILE_get_dir(const char *path) { @@ -300,11 +284,7 @@ PUBLIC const char *FILE_get_dir(const char *path) return "/"; if (file_buffer != path) -#ifdef OS_OPENBSD strlcpy(file_buffer, path, sizeof(file_buffer)); -#else - strcpy(file_buffer, path); -#endif p = rindex(file_buffer, '/'); @@ -315,11 +295,7 @@ PUBLIC const char *FILE_get_dir(const char *path) *p = 0; if (file_buffer[0] == 0 && path[0] == '/') -#ifdef OS_OPENBSD strlcpy(file_buffer, "/", sizeof(file_buffer)); -#else - strcpy(file_buffer, "/"); -#endif } file_buffer_length = -1; @@ -361,11 +337,7 @@ PUBLIC const char *FILE_set_ext(const char *path, const char *ext) if (path != file_buffer) { -#ifdef OS_OPENBSD strlcpy(file_buffer, path, sizeof(file_buffer)); -#else - strcpy(file_buffer, path); -#endif path = file_buffer; } @@ -389,11 +361,7 @@ PUBLIC const char *FILE_set_ext(const char *path, const char *ext) if (*ext == '.') ext++; -#ifdef OS_OPENBSD strlcpy(p, ext, (&file_buffer[MAX_PATH] - p)); -#else - strcpy(p, ext); -#endif file_buffer_length = -1; return path; @@ -407,11 +375,7 @@ PUBLIC const char *FILE_get_basename(const char *path) path = FILE_get_name(path); if (file_buffer != path) -#ifdef OS_OPENBSD strlcpy(file_buffer, path, sizeof(file_buffer)); -#else - strcpy(file_buffer, path); -#endif p = rindex(file_buffer, '.'); if (p) @@ -574,11 +538,7 @@ PUBLIC bool FILE_dir_next(char **path, int *len) if (file_attr) { -#ifdef OS_OPENBSD - strlcpy(p, file_path, &file_buffer[MAX_PATH] - p); -#else - strcpy(p, file_path); -#endif + strlcpy(p, file_path, (&file_buffer[MAX_PATH] - p)); p += strlen(file_path); if (p[-1] != '/' && (file_buffer[1] || file_buffer[0] != '/')) *p++ = '/'; @@ -598,11 +558,7 @@ PUBLIC bool FILE_dir_next(char **path, int *len) if (file_attr) { -#ifdef OS_OPENBSD - strlcpy(p, entry->d_name, &file_buffer[MAX_PATH] - p); -#else - strcpy(p, entry->d_name); -#endif + strlcpy(p, entry->d_name, (&file_buffer[MAX_PATH] - p)); stat(file_buffer, &info); if ((file_attr == GB_STAT_DIRECTORY) ^ (S_ISDIR(info.st_mode) != 0)) continue; @@ -716,11 +672,7 @@ PUBLIC void FILE_make_path_dir(const char *path) return; if (path != file_buffer) -#ifdef OS_OPENBSD strlcpy(file_buffer, path, sizeof(file_buffer)); -#else - strcpy(file_buffer, path); -#endif for (i = 1;; i++) { diff --git a/main/share/gb_replace_temp.h b/main/share/gb_replace_temp.h index 0aeb3ffe6..61c84f670 100644 --- a/main/share/gb_replace_temp.h +++ b/main/share/gb_replace_temp.h @@ -33,6 +33,7 @@ int setenv(const char *name, const char *value, int overwrite) { char *env; + int env_size; if (!name || *name == 0) return (-1); @@ -43,17 +44,14 @@ int setenv(const char *name, const char *value, int overwrite) return 0; } - env = malloc(strlen(name) + strlen(value) + 2); + env_size = strlen(name) + strlen(value) + 2; + env = malloc(env_size); if (!env) return (-1); -#ifdef OS_OPENBSD - strlcpy(env, name, strlen(name) + strlen(value) + 2); -#else - strcpy(env, name); -#endif - strcat(env, "="); - strcat(env, value); + strlcpy(env, name, env_size); + strlcat(env, "=", env_size); + strlcat(env, value, env_size); putenv(env); return 0; diff --git a/main/share/gb_table_temp.h b/main/share/gb_table_temp.h index f8a513b0a..9c0abc7c8 100644 --- a/main/share/gb_table_temp.h +++ b/main/share/gb_table_temp.h @@ -332,7 +332,7 @@ PUBLIC const char *TABLE_get_symbol_name(TABLE *table, int index) PUBLIC const char *TABLE_get_symbol_name_suffix(TABLE *table, int index, const char* suffix) { TABLE_get_symbol_name(table, index); - strcat(_buffer, suffix); + strlcat(_buffer, suffix, sizeof(_buffer)); return _buffer; }