8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-02-02 10:40:38 +01:00

Cleanup - use fb_utils::copy_terminate() where needed

This commit is contained in:
Alexander Peshkov 2016-04-25 18:39:02 +03:00
parent cee67bffae
commit 1adadd969f
18 changed files with 31 additions and 74 deletions

View File

@ -39,6 +39,7 @@
#include "../common/classes/array.h"
#include "../common/classes/UserBlob.h"
#include "../alice/alice_proto.h"
#include "../common/utils_proto.h"
#include <string.h>
@ -411,8 +412,7 @@ static void parse_fullpath(tdr* trans)
trans->tdr_filename = q + 1;
trans->tdr_remote_site = FB_NEW_RPT(*tdgbl->getDefaultPool(), q - p + 1) alice_str;
strncpy((char*) trans->tdr_remote_site->str_data, (char*) p, q - p);
trans->tdr_remote_site->str_data[q - p] = '\0';
fb_utils::copy_terminate((char*) trans->tdr_remote_site->str_data, (char*) p, q - p + 1);
}
}
else

View File

@ -225,13 +225,7 @@ int SecurityDatabaseManagement::release()
return 1;
}
#define STR_STORE(to, from) strStore(to, from, sizeof(to))
static inline void strStore(char* to, const char* from, size_t len)
{
to[--len] = '\0';
strncpy(to, from, len);
}
#define STR_STORE(to, from) fb_utils::copy_terminate(to, from, sizeof(to))
int SecurityDatabaseManagement::execute(Firebird::CheckStatusWrapper* st, Firebird::IUser* user,
Firebird::IListUsers* callback)

View File

@ -229,8 +229,7 @@ bool SecurityDatabase::lookup_user(const char* user_name, char* pwd)
if (pwd)
*pwd = '\0';
strncpy(uname, user_name, sizeof uname);
uname[sizeof uname - 1] = 0;
fb_utils::copy_terminate(uname, user_name, sizeof uname);
MutexLockGuard guard(mutex, FB_FUNCTION);
@ -260,8 +259,7 @@ bool SecurityDatabase::lookup_user(const char* user_name, char* pwd)
if (pwd)
{
strncpy(pwd, user.password, MAX_LEGACY_PASSWORD_LENGTH);
pwd[MAX_LEGACY_PASSWORD_LENGTH] = 0;
fb_utils::copy_terminate(pwd, user.password, MAX_LEGACY_PASSWORD_LENGTH + 1);
}
}

View File

@ -1116,10 +1116,7 @@ void DSC_get_dtype_name(const dsc* desc, TEXT * buffer, USHORT len)
* Convert a datatype to its textual representation
*
**************************************/
// This function didn't put a string terminator even though
// it's calling strncpy that doesn't put it if source > target.
strncpy(buffer, DSC_dtype_tostring(desc->dsc_dtype), len);
buffer[len - 1] = 0;
fb_utils::copy_terminate(buffer, DSC_dtype_tostring(desc->dsc_dtype), len);
}

View File

@ -249,10 +249,7 @@ TEXT* ISC_get_host(TEXT* string, USHORT length)
struct utsname name;
if (uname(&name) >= 0)
{
strncpy(string, name.nodename, length);
string[length - 1] = 0;
}
fb_utils::copy_terminate(string, name.nodename, length);
else
strcpy(string, "local");

View File

@ -1244,8 +1244,7 @@ static void expand_share_name(tstring& share_name)
return;
}
strncpy(workspace, p, sizeof(workspace));
workspace[sizeof(workspace) - 1] = 0;
fb_utils::copy_terminate(workspace, p, sizeof(workspace));
// We test for *q, too, to avoid buffer overrun.
TEXT* q;
for (q = workspace; *q && *p && *p != '!'; p++, q++);

View File

@ -800,9 +800,9 @@ int Parser::yylexAux()
if (charlen > 8)
{
char cbuff[32];
fb_assert(charlen <= 16); // charlen is always <= 16, see 10-15 lines upper
cbuff[0] = 'X';
strncpy(&cbuff[1], hexstring, charlen);
cbuff[charlen + 1] = '\0';
fb_utils::copy_terminate(&cbuff[1], hexstring, charlen + 1);
char* p = &cbuff[1];
UCHAR byte = 0;

View File

@ -1135,7 +1135,7 @@ static void gen_blr(void* /*user_arg*/, SSHORT /*offset*/, const char* string)
{
char d = 0;
if (p1 < q && ((d = *p1++) == '_' || d == '$'))
strncpy(q1 - 4, "isc", 3);
memcpy(q1 - 4, "isc", 3);
else if (d)
*q1++ = d;
}

View File

@ -532,8 +532,7 @@ int main(int argc, char* argv[])
{
gpreGlob.global_db_count = 1;
char* dbn = gpreGlob.global_db_list[0].dbd_name;
strncpy(dbn, db->dbd_name->sym_string, dbd::dbd_size);
dbn[dbd::dbd_size - 1] = 0;
fb_utils::copy_terminate(dbn, db->dbd_name->sym_string, dbd::dbd_size);
}
#endif
break;

View File

@ -66,6 +66,7 @@
#include "dyn_consts.h"
#include "../jrd/ibase.h"
#include "../jrd/constants.h"
#include "../common/utils_proto.h"
#ifdef GPRE_FORTRAN
#if defined AIX || defined AIX_PPC || defined __sun
@ -1621,8 +1622,7 @@ public:
}
gpre_exception(const char* errmsg)
{
strncpy(msg, errmsg, sizeof(msg));
msg[sizeof(msg) - 1] = 0;
fb_utils::copy_terminate(msg, errmsg, sizeof(msg));
}
const char* what() const throw()
{

View File

@ -1147,7 +1147,7 @@ static void gen_blr(void* /*user_arg*/, SSHORT /*offset*/, const char* string)
{
char d = 0;
if (p1 < q && ((d = *p1++) == '_' || d == '$'))
strncpy(q1 - 4, "isc", 3);
memcpy(q1 - 4, "isc", 3);
else if (d)
*q1++ = d;
}

View File

@ -617,8 +617,7 @@ act* PAR_database(bool sql, const TEXT* base_directory)
strcat(s, ".");
if (!gpreGlob.ada_package[0] || !strcmp(gpreGlob.ada_package, s))
{
strncpy(gpreGlob.ada_package, s, MAXPATHLEN);
gpreGlob.ada_package[MAXPATHLEN - 1] = 0;
fb_utils::copy_terminate(gpreGlob.ada_package, s, MAXPATHLEN);
}
else
{

View File

@ -5477,8 +5477,7 @@ static gpre_index* make_index( gpre_req* request, const TEXT* string)
// CVC: I've kept this silly code. What's the idea of the copy here?
// If we are trying to limit the index name, the correct length is NAME_SIZE.
TEXT s[ERROR_LENGTH];
strncpy(s, string, sizeof(s));
s[sizeof(s) - 1] = 0;
fb_utils::copy_terminate(s, string, sizeof(s));
gpre_index* index = MET_make_index(s);
if (request)
request->req_database = gpreGlob.isc_databases;
@ -5501,8 +5500,7 @@ static gpre_rel* make_relation( gpre_req* request, const TEXT* relation_name)
if (gpreGlob.isc_databases && !gpreGlob.isc_databases->dbb_next)
{
TEXT r[ERROR_LENGTH];
strncpy(r, relation_name, sizeof(r));
r[sizeof(r) - 1] = 0;
fb_utils::copy_terminate(r, relation_name, sizeof(r));
gpre_rel* relation = MET_make_relation(r);
relation->rel_database = gpreGlob.isc_databases;
@ -6793,8 +6791,7 @@ void SQL_resolve_identifier( const TEXT* err_mesg, TEXT* str_in, int in_size)
// or SQL escape sequences in quoted identifiers.
if (tk_string[0] == '\"')
strip_quotes(gpreGlob.token_global);
strncpy(str, tk_string, len);
str[len] = 0;
fb_utils::copy_terminate(str, tk_string, len + 1);
break;
case tok_ident:
to_upcase(tk_string, str, len + 1);

View File

@ -64,8 +64,7 @@ class MetIdentifier
public:
explicit MetIdentifier(const char* s)
{
strncpy(name, s, NAME_SIZE);
name[NAME_SIZE - 1] = 0;
fb_utils::copy_terminate(name, s, NAME_SIZE);
}
operator char*() {return name;}
operator const char*() const {return name;}

View File

@ -2185,8 +2185,7 @@ static processing_state add_row(TEXT* tabname)
// Convert first 4 chars to upper case for comparison
SCHAR cmd[5];
strncpy(cmd, lastInputLine, 4);
cmd[4] = '\0';
fb_utils::copy_terminate(cmd, lastInputLine, sizeof cmd);
IUTILS_make_upper(cmd);
// If the user writes NULL, put a null in the column
@ -2936,8 +2935,7 @@ static processing_state bulk_insert_hack(const char* command)
else
{
// Convert first 4 chars to upper case for comparison.
strncpy(cmd, lastPos, 4);
cmd[4] = '\0';
fb_utils::copy_terminate(cmd, lastPos, sizeof cmd);
IUTILS_make_upper(cmd);
}

View File

@ -159,10 +159,7 @@ void ERR_log(int facility, int number, const TEXT* message)
DEBUG;
if (message)
{
strncpy(errmsg, message, sizeof(errmsg));
errmsg[sizeof(errmsg) - 1] = 0;
}
fb_utils::copy_terminate(errmsg, message, sizeof(errmsg));
else if (gds__msg_lookup(0, facility, number, sizeof(errmsg), errmsg, NULL) < 1)
strcpy(errmsg, "Internal error code");

View File

@ -900,10 +900,7 @@ static SLONG safe_interpret(char* const s, const FB_SIZE_T bufsize,
if (legacy)
safe_strncpy(s, q, bufsize);
else
{
strncpy(s, q, bufsize);
s[bufsize - 1] = 0;
}
fb_utils::copy_terminate(s, q, bufsize);
break;
case isc_arg_unix:
@ -913,10 +910,7 @@ static SLONG safe_interpret(char* const s, const FB_SIZE_T bufsize,
if (legacy)
safe_strncpy(s, q, bufsize);
else
{
strncpy(s, q, bufsize);
s[bufsize - 1] = 0;
}
fb_utils::copy_terminate(s, q, bufsize);
break;
case isc_arg_dos:
@ -2482,8 +2476,7 @@ BOOLEAN API_ROUTINE gds__validate_lib_path(const TEXT* module,
Firebird::string ib_ext_lib_path;
if (!fb_utils::readenv(ib_env_var, ib_ext_lib_path))
{
strncpy(resolved_module, module, length);
resolved_module[length - 1] = 0;
fb_utils::copy_terminate(resolved_module, module, length);
return TRUE; // The variable is not defined. Return TRUE
}
@ -2513,16 +2506,14 @@ BOOLEAN API_ROUTINE gds__validate_lib_path(const TEXT* module,
const TEXT* token = strtok(ib_ext_lib_path.begin(), ";");
while (token != NULL)
{
strncpy(path, token, sizeof(path));
path[sizeof(path) - 1] = 0;
fb_utils::copy_terminate(path, token, sizeof(path));
// make sure that there is no traing slash on the path
TEXT* p = path + strlen(path);
if ((p != path) && ((p[-1] == '/') || (p[-1] == '\\')))
p[-1] = 0;
if ((EXPAND_PATH(path, abs_path)) && (!COMPARE_PATH(abs_path, abs_module_path)))
{
strncpy(resolved_module, abs_module, length);
resolved_module[length - 1] = 0;
fb_utils::copy_terminate(resolved_module, abs_module, length);
return TRUE;
}
token = strtok(NULL, ";");
@ -3708,8 +3699,7 @@ void FB_EXPORTED gds__trace_printer(void* /*arg*/, SSHORT offset, const TEXT* li
gds__ulstr(p, offset, 4, ' ');
p += strlen(p);
*p++ = ' ';
strncpy(p, line, PRETTY_BUFFER_SIZE);
p[PRETTY_BUFFER_SIZE] = '\0';
fb_utils::copy_terminate(p, line, PRETTY_BUFFER_SIZE + 1);
p += strlen(p);
*p++ = '\n';
*p = 0;

View File

@ -2066,16 +2066,9 @@ void API_ROUTINE isc_format_implementation(USHORT impl_nr,
if (cbuflen > 0)
{
if (impl_class_nr >= FB_NELEM(impl_class) || !(impl_class[impl_class_nr]))
{
strncpy(cbuf, "**unknown**", cbuflen - 1);
cbuf[MIN(11, cbuflen - 1)] = '\0';
}
fb_utils::copy_terminate(cbuf, "**unknown**", cbuflen);
else
{
strncpy(cbuf, impl_class[impl_class_nr], cbuflen - 1);
const int len = static_cast<int>(strlen(impl_class[impl_class_nr]));
cbuf[MIN(len, cbuflen - 1)] = '\0';
}
fb_utils::copy_terminate(cbuf, impl_class[impl_class_nr], cbuflen);
}
}