Changeset: 297a30e78198 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=297a30e78198
Added Files:
common/utils/mstring.c
common/utils/mstring.h
Modified Files:
clients/examples/C/Makefile.ag
clients/mapiclient/Makefile.ag
clients/mapiclient/dump.c
clients/mapiclient/msqldump.c
clients/mapilib/mapi.c
clients/odbc/driver/Makefile.ag
clients/odbc/driver/ODBCError.c
clients/odbc/driver/ODBCGlobal.h
clients/odbc/driver/ODBCUtil.c
clients/odbc/driver/ODBCUtil.h
clients/odbc/driver/SQLBrowseConnect.c
clients/odbc/driver/SQLConnect.c
clients/odbc/driver/SQLDriverConnect.c
clients/odbc/driver/SQLGetDiagField.c
clients/odbc/driver/SQLGetDiagRec.c
clients/odbc/setup/Makefile.ag
clients/odbc/setup/drvcfg.c
common/utils/Makefile.ag
common/utils/mutils.c
gdk/gdk.h
gdk/gdk_align.c
gdk/gdk_atoms.c
gdk/gdk_bat.c
gdk/gdk_batop.c
gdk/gdk_bbp.c
gdk/gdk_cand.c
gdk/gdk_hash.c
gdk/gdk_heap.c
gdk/gdk_imprints.c
gdk/gdk_logger.c
gdk/gdk_orderidx.c
gdk/gdk_private.h
gdk/gdk_project.c
gdk/gdk_storage.c
gdk/gdk_string.c
gdk/gdk_system.c
gdk/gdk_system.h
gdk/gdk_utils.c
geom/monetdb5/geom.c
monetdb5/mal/mal_atom.c
monetdb5/mal/mal_client.c
monetdb5/mal/mal_debugger.c
monetdb5/mal/mal_exception.c
monetdb5/mal/mal_instruction.c
monetdb5/modules/atoms/json.c
monetdb5/modules/atoms/str.c
monetdb5/modules/atoms/url.c
monetdb5/modules/mal/clients.c
sql/backends/monet5/vaults/bam/bam_wrapper.c
tools/merovingian/daemon/argvcmds.c
tools/merovingian/daemon/connections.c
tools/merovingian/daemon/proxy.c
tools/merovingian/utils/control.c
Branch: default
Log Message:
Introduce new functions strcpy_len and strconcat_len to copy strings safely.
These functions take a destination buffer and the size of said buffer
and copy the source string (or a NULL-terminated list of source
strings) to the buffer, making sure there is no buffer overflow and
the result buffer is NULL terminated. The functions return the length
of the (combined) source string(s), so if that length is larger than
or equal to the size of the destination buffer, the string(s)
was(were) truncated.
Note, strcpy_len is similar to the BSD/Solaris function strlcpy.
diffs (truncated from 1936 to 300 lines):
diff --git a/clients/examples/C/Makefile.ag b/clients/examples/C/Makefile.ag
--- a/clients/examples/C/Makefile.ag
+++ b/clients/examples/C/Makefile.ag
@@ -14,5 +14,6 @@ BINS = {
smack00.c smack01.c
LIBS = ../../mapilib/libmapi \
../../../common/stream/libstream \
+ ../../../common/utils/libmutils \
$(SOCKET_LIBS) $(zlib_LIBS) $(bzip2_LIBS) $(snappy_LIBS)
$(lz4_LIBS) $(liblzma_LIBS) $(curl_LIBS) $(LTLIBICONV) $(openssl_LIBS)
}
diff --git a/clients/mapiclient/Makefile.ag b/clients/mapiclient/Makefile.ag
--- a/clients/mapiclient/Makefile.ag
+++ b/clients/mapiclient/Makefile.ag
@@ -15,42 +15,46 @@ lib_mcutil = {
bin_mclient = {
SOURCES = mclient.c ReadlineTools.c ReadlineTools.h mhelp.c mhelp.h
LIBS = libmcutil \
- ../../common/utils/libmutils \
../mapilib/libmapi \
../../common/stream/libstream \
+ ../../common/utils/libmutils \
$(READLINE_LIBS) \
$(SOCKET_LIBS) $(zlib_LIBS) $(bzip2_LIBS) $(snappy_LIBS)
$(lz4_LIBS) $(liblzma_LIBS) $(curl_LIBS) $(LTLIBICONV) $(openssl_LIBS)
}
bin_msqldump = {
SOURCES = msqldump.c msqldump.h
- LIBS = libmcutil ../../common/utils/libmutils \
+ LIBS = libmcutil \
../mapilib/libmapi \
../../common/stream/libstream \
+ ../../common/utils/libmutils \
$(SOCKET_LIBS) $(zlib_LIBS) $(bzip2_LIBS) $(snappy_LIBS)
$(lz4_LIBS) $(liblzma_LIBS) $(curl_LIBS) $(LTLIBICONV) $(openssl_LIBS)
}
bin_stethoscope = {
SOURCES = stethoscope.c
- LIBS = libmcutil ../../common/utils/libmutils \
+ LIBS = libmcutil \
../mapilib/libmapi \
../../common/stream/libstream \
+ ../../common/utils/libmutils \
$(SOCKET_LIBS) $(zlib_LIBS) $(bzip2_LIBS) $(snappy_LIBS)
$(lz4_LIBS) $(liblzma_LIBS) $(curl_LIBS) $(LTLIBICONV) $(openssl_LIBS)
}
bin_tachograph = {
SOURCES = tachograph.c
- LIBS = libmcutil ../../common/utils/libmutils \
+ LIBS = libmcutil \
../mapilib/libmapi \
../../common/stream/libstream \
+ ../../common/utils/libmutils \
$(SOCKET_LIBS) $(zlib_LIBS) $(bzip2_LIBS) $(snappy_LIBS)
$(lz4_LIBS) $(liblzma_LIBS) $(curl_LIBS) $(LTLIBICONV) $(openssl_LIBS)
}
bin_tomograph = {
SOURCES = tomograph.c
- LIBS = libmcutil ../../common/utils/libmutils \
+ LIBS = libmcutil \
../mapilib/libmapi \
../../common/stream/libstream \
+ ../../common/utils/libmutils \
$(SOCKET_LIBS) $(zlib_LIBS) $(bzip2_LIBS) $(snappy_LIBS)
$(lz4_LIBS) $(liblzma_LIBS) $(curl_LIBS) $(LTLIBICONV) $(openssl_LIBS)
}
diff --git a/clients/mapiclient/dump.c b/clients/mapiclient/dump.c
--- a/clients/mapiclient/dump.c
+++ b/clients/mapiclient/dump.c
@@ -9,6 +9,7 @@
#include "monetdb_config.h"
#include "mapi.h"
#include "stream.h"
+#include "mstring.h"
#include <unistd.h>
#include <string.h>
#include <ctype.h>
@@ -1147,14 +1148,13 @@ describe_table(Mapi mid, const char *sch
if (schema == NULL) {
if ((sname = strchr(tname, '.')) != NULL) {
- size_t len = sname - tname;
+ size_t len = sname - tname + 1;
- sname = malloc(len + 1);
+ sname = malloc(len);
if (sname == NULL)
goto bailout;
- strncpy(sname, tname, len);
- sname[len] = 0;
- tname += len + 1;
+ strcpy_len(sname, tname, len);
+ tname += len;
} else if ((sname = get_schema(mid)) == NULL) {
return 1;
}
@@ -1489,14 +1489,13 @@ describe_sequence(Mapi mid, const char *
if (schema == NULL) {
if ((sname = strchr(tname, '.')) != NULL) {
- size_t len = sname - tname;
+ size_t len = sname - tname + 1;
- sname = malloc(len + 1);
+ sname = malloc(len);
if (sname == NULL)
goto bailout;
- strncpy(sname, tname, len);
- sname[len] = 0;
- tname += len + 1;
+ strcpy_len(sname, tname, len);
+ tname += len;
} else if ((sname = get_schema(mid)) == NULL) {
return 1;
}
@@ -1657,14 +1656,13 @@ dump_table_data(Mapi mid, const char *sc
if (schema == NULL) {
if ((sname = strchr(tname, '.')) != NULL) {
- size_t len = sname - tname;
+ size_t len = sname - tname + 1;
- sname = malloc(len + 1);
+ sname = malloc(len);
if (sname == NULL)
goto bailout;
- strncpy(sname, tname, len);
- sname[len] = 0;
- tname += len + 1;
+ strcpy_len(sname, tname, len);
+ tname += len;
} else if ((sname = get_schema(mid)) == NULL) {
goto bailout;
}
@@ -2169,14 +2167,13 @@ dump_functions(Mapi mid, stream *toConso
/* no schema given, so figure it out */
const char *dot = strchr(fname, '.');
if (dot != NULL) {
- size_t len = dot - fname;
+ size_t len = dot - fname + 1;
- to_free = malloc(len + 1);
+ to_free = malloc(len);
if (to_free == NULL)
goto bailout;
- strncpy(to_free, fname, len);
- to_free[len] = 0;
- fname += len + 1;
+ strcpy_len(to_free, fname, len);
+ fname += len;
} else if ((to_free = get_schema(mid)) == NULL) {
return 1;
}
diff --git a/clients/mapiclient/msqldump.c b/clients/mapiclient/msqldump.c
--- a/clients/mapiclient/msqldump.c
+++ b/clients/mapiclient/msqldump.c
@@ -204,7 +204,7 @@ main(int argc, char **argv)
#ifdef HAVE_CTIME_R
ctime_r(&t, buf);
#else
- strncpy(buf, ctime(&t), sizeof(buf));
+ strcpy_len(buf, ctime(&t), sizeof(buf));
#endif
#endif
if ((p = strrchr(buf, '\n')) != NULL)
diff --git a/clients/mapilib/mapi.c b/clients/mapilib/mapi.c
--- a/clients/mapilib/mapi.c
+++ b/clients/mapilib/mapi.c
@@ -689,6 +689,7 @@
#include "mapi.h"
#include "mcrypt.h"
#include "matomic.h"
+#include "mstring.h"
#ifdef HAVE_UNISTD_H
# include <unistd.h>
@@ -1580,8 +1581,8 @@ add_error(struct MapiResultSet *result,
(error[4] >= 'A' && error[4] <= 'Z'))) {
if (result->errorstr == NULL) {
/* remeber SQLSTATE for first error */
- strncpy(result->sqlstate, error, 5);
- result->sqlstate[5] = 0;
+ strcpy_len(result->sqlstate, error,
+ sizeof(result->sqlstate));
}
/* skip SQLSTATE */
error += 6;
@@ -2333,8 +2334,7 @@ mapi_reconnect(Mapi mid)
userver = (struct sockaddr_un) {
.sun_family = AF_UNIX,
};
- strncpy(userver.sun_path, mid->hostname,
sizeof(userver.sun_path) - 1);
- userver.sun_path[sizeof(userver.sun_path) - 1] = 0;
+ strcpy_len(userver.sun_path, mid->hostname,
sizeof(userver.sun_path));
if (connect(s, serv, sizeof(struct sockaddr_un)) ==
SOCKET_ERROR) {
snprintf(errbuf, sizeof(errbuf),
@@ -3372,7 +3372,7 @@ mapi_param_store(MapiHdl hdl)
if (hdl->query == NULL)
return;
}
- strncpy(hdl->query + k, p, q - p);
+ memcpy(hdl->query + k, p, q - p);
k += q - p;
hdl->query[k] = 0;
@@ -4454,8 +4454,7 @@ mapi_query_part(MapiHdl hdl, const char
if (hdl->query == NULL) {
hdl->query = malloc(size + 1);
if (hdl->query) {
- strncpy(hdl->query, query, size);
- hdl->query[size] = 0;
+ strcpy_len(hdl->query, query, size + 1);
}
} else {
size_t sz = strlen(hdl->query);
@@ -4463,8 +4462,7 @@ mapi_query_part(MapiHdl hdl, const char
if (sz < 512 &&
(q = realloc(hdl->query, sz + size + 1)) != NULL) {
- strncpy(q + sz, query, size);
- q[sz + size] = 0;
+ strcpy_len(q + sz, query, size + 1);
hdl->query = q;
}
}
@@ -4827,10 +4825,8 @@ unquote(const char *msg, char **str, con
}
len = s - msg;
*str = malloc(len + 1);
- strncpy(*str, msg, len);
-
- /* make sure value is NULL terminated */
- (*str)[len] = 0;
+ strcpy_len(*str, msg, len + 1);
+
if (next)
*next = p;
if (lenp)
diff --git a/clients/odbc/driver/Makefile.ag b/clients/odbc/driver/Makefile.ag
--- a/clients/odbc/driver/Makefile.ag
+++ b/clients/odbc/driver/Makefile.ag
@@ -6,6 +6,7 @@
INCLUDES = \
../../mapilib \
+ ../../../common/utils \
$(ODBC_INCS)
DEFS = @DEFS@
@@ -106,7 +107,7 @@ lib_MonetODBC = {
SQLTransact.c \
driver.rc \
ODBC.syms
- LIBS = ../../mapilib/libmapi ../../../common/stream/libstream
$(LTLIBICONV) $(ODBCINST_LIBS) $(SOCKET_LIBS) $(openssl_LIBS)
+ LIBS = ../../mapilib/libmapi ../../../common/stream/libstream
../../../common/utils/libmutils $(LTLIBICONV) $(ODBCINST_LIBS) $(SOCKET_LIBS)
$(openssl_LIBS)
}
EXTRA_DIST = Copyright
diff --git a/clients/odbc/driver/ODBCError.c b/clients/odbc/driver/ODBCError.c
--- a/clients/odbc/driver/ODBCError.c
+++ b/clients/odbc/driver/ODBCError.c
@@ -240,14 +240,10 @@ newODBCError(const char *SQLState, const
}
if (SQLState) {
- strncpy(error->sqlState, SQLState, SQL_SQLSTATE_SIZE);
- error->sqlState[SQL_SQLSTATE_SIZE] = '\0';
+ strcpy_len(error->sqlState, SQLState, sizeof(error->sqlState));
} else {
/* initialize it with nulls */
- int i = 0;
-
- for (; i <= SQL_SQLSTATE_SIZE; i++)
- error->sqlState[i] = 0;
+ memset(error->sqlState, 0, sizeof(error->sqlState));
}
if (msg) {
diff --git a/clients/odbc/driver/ODBCGlobal.h b/clients/odbc/driver/ODBCGlobal.h
--- a/clients/odbc/driver/ODBCGlobal.h
+++ b/clients/odbc/driver/ODBCGlobal.h
@@ -32,6 +32,7 @@
#define _H_ODBCGLOBAL
#include "monetdb_config.h"
+#include "mstring.h"
/**** Define the ODBC Version this ODBC driver complies with ****/
#define ODBCVER 0x0352 /* Important: this must be defined before
include of sqlext.h */
diff --git a/clients/odbc/driver/ODBCUtil.c b/clients/odbc/driver/ODBCUtil.c
--- a/clients/odbc/driver/ODBCUtil.c
+++ b/clients/odbc/driver/ODBCUtil.c
@@ -64,8 +64,7 @@ dupODBCstring(const SQLCHAR *inStr, size
if (tmp == NULL)
return NULL;
- strncpy(tmp, (const char *) inStr, length);
- tmp[length] = '\0'; /* make it null terminated */
+ strcpy_len(tmp, (const char *) inStr, length + 1);
return tmp;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list