summaryrefslogtreecommitdiffstats
path: root/ini
diff options
context:
space:
mode:
authorDmitri Pal <dpal@redhat.com>2012-12-23 13:49:56 -0500
committerOndrej Kos <okos@redhat.com>2013-01-24 08:34:40 +0100
commit833a46e384828be48c27898e755d6215eb5c4bb8 (patch)
treeeaf34cd463381efc40991151d41d9deec295f704 /ini
parent57faa64667411a3d8eeeeff62b1e659a94ed29cf (diff)
downloadding-libs2-833a46e384828be48c27898e755d6215eb5c4bb8.tar.gz
ding-libs2-833a46e384828be48c27898e755d6215eb5c4bb8.tar.xz
ding-libs2-833a46e384828be48c27898e755d6215eb5c4bb8.zip
Replacing sprintf with snprintf
Replaced sprintf in the unit test. Defined constants for sizes and used them. Wrpapped lines where noticed that they are longer than 80. Added comments to the places where sprintf is still used but it is safe to use.
Diffstat (limited to 'ini')
-rw-r--r--ini/ini_config.c1
-rw-r--r--ini/ini_config_ut.c6
-rw-r--r--ini/ini_parse_ut.c103
-rw-r--r--ini/ini_valueobj_ut.c8
4 files changed, 68 insertions, 50 deletions
diff --git a/ini/ini_config.c b/ini/ini_config.c
index 4265f38..c0a908b 100644
--- a/ini/ini_config.c
+++ b/ini/ini_config.c
@@ -911,6 +911,7 @@ int config_for_app_with_metadata(const char *application,
return error;
}
+ /* It is safe to use sprintf() here */
sprintf(file_name, "%s%s%s.conf", config_dir, SLASH, application);
TRACE_INFO_STRING("Opening file:", file_name);
/* Read specific file */
diff --git a/ini/ini_config_ut.c b/ini/ini_config_ut.c
index 34b395b..fff3c4e 100644
--- a/ini/ini_config_ut.c
+++ b/ini/ini_config_ut.c
@@ -1547,6 +1547,7 @@ int main(int argc, char *argv[])
char *srcdir = NULL;
char *rundir = NULL;
const char inidir[] = "/ini";
+ int len = 0;
if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = 1;
@@ -1555,14 +1556,15 @@ int main(int argc, char *argv[])
srcdir = getenv("srcdir");
if(srcdir) {
- rundir = malloc(strlen(srcdir) + sizeof(inidir));
+ len = strlen(srcdir) + sizeof(inidir);
+ rundir = malloc(len);
if (!rundir) {
printf("Failed to allocate memory to store path"
" to the test files %d.\n", ENOMEM);
return -1;
}
- sprintf(rundir, "%s%s", srcdir, inidir);
+ snprintf(rundir, len, "%s%s", srcdir, inidir);
errno = 0;
if(chdir(rundir) != 0) {
diff --git a/ini/ini_parse_ut.c b/ini/ini_parse_ut.c
index 5bf45b4..d6f812f 100644
--- a/ini/ini_parse_ut.c
+++ b/ini/ini_parse_ut.c
@@ -38,6 +38,8 @@
int verbose = 0;
char *confdir = NULL;
+#define VAL_SIZE 100
+
#define INIOUT(foo) \
do { \
if (verbose) foo; \
@@ -185,9 +187,9 @@ int read_save_test(void)
while(files[i]) {
- sprintf(infile, "%s/ini/ini.d/%s.conf", (srcdir == NULL) ? "." : srcdir,
- files[i]);
- sprintf(outfile, "./%s.conf.out", files[i]);
+ snprintf(infile, PATH_MAX, "%s/ini/ini.d/%s.conf",
+ (srcdir == NULL) ? "." : srcdir, files[i]);
+ snprintf(outfile, PATH_MAX, "./%s.conf.out", files[i]);
error = test_one_file(infile, outfile);
INIOUT(printf("Test for file: %s returned %d\n", files[i], error));
i++;
@@ -217,17 +219,18 @@ int read_again_test(void)
while(files[i]) {
- sprintf(infile, "./%s.conf.out", files[i]);
- sprintf(outfile, "./%s.conf.2.out", files[i]);
+ snprintf(infile, PATH_MAX, "./%s.conf.out", files[i]);
+ snprintf(outfile, PATH_MAX, "./%s.conf.2.out", files[i]);
error = test_one_file(infile, outfile);
INIOUT(printf("Test for file: %s returned %d\n", files[i], error));
if (error) break;
- sprintf(command,"diff -q %s %s", infile, outfile);
+ snprintf(command, PATH_MAX * 3, "diff -q %s %s", infile, outfile);
error = system(command);
INIOUT(printf("Comparison of %s %s returned: %d\n",
infile, outfile, error));
if ((error) || (WEXITSTATUS(error))) {
- printf("Failed to run copy command %d %d.\n", error, WEXITSTATUS(error));
+ printf("Failed to run copy command %d %d.\n", error,
+ WEXITSTATUS(error));
error = -1;
break;
}
@@ -340,7 +343,7 @@ int merge_values_test(void)
srcdir = getenv("srcdir");
- sprintf(filename, "%s/ini/ini.d/foo.conf.in",
+ snprintf(filename, PATH_MAX, "%s/ini/ini.d/foo.conf.in",
(srcdir == NULL) ? "." : srcdir);
error = simplebuffer_alloc(&sbobj);
@@ -381,7 +384,8 @@ int merge_values_test(void)
error = ini_config_parse(file_ctx,
ini_config);
if (error) {
- INIOUT(printf("Failed to parse configuration. Error %d.\n", error));
+ INIOUT(printf("Failed to parse configuration. Error %d.\n",
+ error));
if (ini_config_error_count(file_ctx)) {
INIOUT(printf("Errors detected while parsing: %s\n",
@@ -391,10 +395,12 @@ int merge_values_test(void)
ini_config_free_errors(error_list);
}
- if (((mflags[i] != INI_MV1S_ERROR) && (mflags[i]!= INI_MV1S_DETECT)) ||
+ if (((mflags[i] != INI_MV1S_ERROR) &&
+ (mflags[i]!= INI_MV1S_DETECT)) ||
((mflags[i] = INI_MV1S_ERROR) && (error != EEXIST)) ||
((mflags[i] = INI_MV1S_DETECT) && (error != EEXIST))) {
- printf("This is unexpected error %d in mode %d\n", error, mflags[i]);
+ printf("This is unexpected error %d in mode %d\n",
+ error, mflags[i]);
ini_config_destroy(ini_config);
simplebuffer_free(sbobj);
ini_config_file_destroy(file_ctx);
@@ -448,12 +454,13 @@ int merge_values_test(void)
return error;
}
- sprintf(command,"diff -q %s %s", resname, checkname);
+ snprintf(command, PATH_MAX * 3, "diff -q %s %s", resname, checkname);
error = system(command);
INIOUT(printf("Comparison of %s %s returned: %d\n",
resname, checkname, error));
if ((error) || (WEXITSTATUS(error))) {
- printf("Failed to run copy command %d %d.\n", error, WEXITSTATUS(error));
+ printf("Failed to run copy command %d %d.\n", error,
+ WEXITSTATUS(error));
return -1;
}
@@ -501,7 +508,7 @@ int merge_section_test(void)
char checkname[PATH_MAX];
char resname[PATH_MAX];
char command[PATH_MAX * 3];
- char mode[100];
+ char mode[VAL_SIZE];
char *srcdir = NULL;
char *builddir = NULL;
@@ -510,11 +517,11 @@ int merge_section_test(void)
srcdir = getenv("srcdir");
builddir = getenv("builddir");
- sprintf(filename, "%s/ini/ini.d/smerge.conf",
+ snprintf(filename, PATH_MAX, "%s/ini/ini.d/smerge.conf",
(srcdir == NULL) ? "." : srcdir);
- sprintf(checkname, "%s/ini/ini.d/sexpect.conf",
+ snprintf(checkname, PATH_MAX, "%s/ini/ini.d/sexpect.conf",
(srcdir == NULL) ? "." : srcdir);
- sprintf(resname, "%s/smerge.conf.out",
+ snprintf(resname, PATH_MAX, "%s/smerge.conf.out",
(builddir == NULL) ? "." : builddir);
error = simplebuffer_alloc(&sbobj);
@@ -529,13 +536,13 @@ int merge_section_test(void)
INIOUT(printf("<==== Testing mode %s + %s ====>\n",
secmstr[i], mstr[j]));
- sprintf(mode, "# Section mode: %s, value mode: %s\n",
+ snprintf(mode, VAL_SIZE, "# Section mode: %s, value mode: %s\n",
secmstr[i], mstr[j]);
error = simplebuffer_add_str(sbobj,
mode,
strlen(mode),
- 100);
+ VAL_SIZE);
if (error) {
TRACE_ERROR_NUMBER("Failed to add string.",
error);
@@ -650,13 +657,14 @@ int merge_section_test(void)
simplebuffer_free(sbobj);
fclose(ff);
- sprintf(command,"diff -q %s %s", resname, checkname);
+ snprintf(command, PATH_MAX * 3, "diff -q %s %s", resname, checkname);
error = system(command);
INIOUT(printf("Comparison of %s %s returned: %d\n",
resname, checkname, error));
if ((error) || (WEXITSTATUS(error))) {
- printf("Failed to run diff command %d %d.\n", error, WEXITSTATUS(error));
+ printf("Failed to run diff command %d %d.\n", error,
+ WEXITSTATUS(error));
return -1;
}
@@ -757,8 +765,8 @@ int merge_file_test(void)
char resname[PATH_MAX];
char checkname[PATH_MAX];
char command[PATH_MAX * 3];
- char msg[100];
- char mode[100];
+ char msg[VAL_SIZE];
+ char mode[VAL_SIZE];
char *srcdir = NULL;
char *builddir = NULL;
uint32_t collision_flags;
@@ -770,16 +778,16 @@ int merge_file_test(void)
srcdir = getenv("srcdir");
builddir = getenv("builddir");
- sprintf(firstname, "%s/ini/ini.d/first.conf",
+ snprintf(firstname, PATH_MAX, "%s/ini/ini.d/first.conf",
(srcdir == NULL) ? "." : srcdir);
- sprintf(secondname, "%s/ini/ini.d/second.conf",
+ snprintf(secondname, PATH_MAX, "%s/ini/ini.d/second.conf",
(srcdir == NULL) ? "." : srcdir);
- sprintf(checkname, "%s/ini/ini.d/mergecheck.conf",
+ snprintf(checkname, PATH_MAX, "%s/ini/ini.d/mergecheck.conf",
(srcdir == NULL) ? "." : srcdir);
- sprintf(resname, "%s/mergecheck.conf.out",
+ snprintf(resname, PATH_MAX, "%s/mergecheck.conf.out",
(builddir == NULL) ? "." : builddir);
error = simplebuffer_alloc(&sbobj);
@@ -794,13 +802,13 @@ int merge_file_test(void)
INIOUT(printf("<==== Testing mode %s + %s ====>\n",
secmstr[i], mstr[j]));
- sprintf(mode, "# Section mode: %s, value mode: %s\n",
+ snprintf(mode, VAL_SIZE, "# Section mode: %s, value mode: %s\n",
secmstr[i], mstr[j]);
error = simplebuffer_add_str(sbobj,
mode,
strlen(mode),
- 100);
+ VAL_SIZE);
if (error) {
TRACE_ERROR_NUMBER("Failed to add string.",
error);
@@ -923,7 +931,7 @@ int merge_file_test(void)
error = simplebuffer_add_str(sbobj,
msg,
strlen(msg),
- 100);
+ VAL_SIZE);
if (error) {
TRACE_ERROR_NUMBER("Failed to add string.",
error);
@@ -952,7 +960,7 @@ int merge_file_test(void)
error = simplebuffer_add_str(sbobj,
msg,
strlen(msg),
- 100);
+ VAL_SIZE);
if (error) {
TRACE_ERROR_NUMBER("Failed to add string.",
error);
@@ -1017,13 +1025,14 @@ int merge_file_test(void)
simplebuffer_free(sbobj);
fclose(ff);
- sprintf(command,"diff -q %s %s", resname, checkname);
+ snprintf(command,PATH_MAX * 3, "diff -q %s %s", resname, checkname);
error = system(command);
INIOUT(printf("Comparison of %s %s returned: %d\n",
resname, checkname, error));
if ((error) || (WEXITSTATUS(error))) {
- printf("Failed to run diff command %d %d.\n", error, WEXITSTATUS(error));
+ printf("Failed to run diff command %d %d.\n", error,
+ WEXITSTATUS(error));
return -1;
}
@@ -1040,24 +1049,26 @@ int startup_test(void)
char **error_list = NULL;
char infile[PATH_MAX];
char outfile[PATH_MAX];
- char command[PATH_MAX * 2 + 3];
+ char command[PATH_MAX * 3];
char *srcdir = NULL;
char *builddir;
INIOUT(printf("<==== Startup test ====>\n"));
srcdir = getenv("srcdir");
- sprintf(infile, "%s/ini/ini.d/foo.conf.in", (srcdir == NULL) ? "." : srcdir);
+ snprintf(infile, PATH_MAX, "%s/ini/ini.d/foo.conf.in",
+ (srcdir == NULL) ? "." : srcdir);
builddir = getenv("builddir");
- sprintf(outfile, "%s/foo.conf",
+ snprintf(outfile, PATH_MAX, "%s/foo.conf",
(builddir == NULL) ? "." : builddir);
- sprintf(command, "cp %s %s", infile, outfile);
+ snprintf(command, PATH_MAX * 3, "cp %s %s", infile, outfile);
INIOUT(printf("Running command '%s'\n", command));
error = system(command);
if ((error) || (WEXITSTATUS(error))) {
- printf("Failed to run copy command %d %d.\n", error, WEXITSTATUS(error));
+ printf("Failed to run copy command %d %d.\n", error,
+ WEXITSTATUS(error));
return -1;
}
@@ -1157,7 +1168,7 @@ int reload_test(void)
struct ini_cfgfile *file_ctx_new = NULL;
char infile[PATH_MAX];
char outfile[PATH_MAX];
- char command[PATH_MAX * 2 + 3];
+ char command[PATH_MAX * 3];
char *srcdir;
char *builddir;
int changed = 0;
@@ -1165,18 +1176,19 @@ int reload_test(void)
INIOUT(printf("<==== Reload test ====>\n"));
srcdir = getenv("srcdir");
- sprintf(infile, "%s/ini/ini.d/foo.conf.in",
+ snprintf(infile, PATH_MAX, "%s/ini/ini.d/foo.conf.in",
(srcdir == NULL) ? "." : srcdir);
builddir = getenv("builddir");
- sprintf(outfile, "%s/foo.conf",
+ snprintf(outfile, PATH_MAX, "%s/foo.conf",
(builddir == NULL) ? "." : builddir);
- sprintf(command, "cp %s %s", infile, outfile);
+ snprintf(command, PATH_MAX * 3, "cp %s %s", infile, outfile);
INIOUT(printf("Running command '%s'\n", command));
error = system(command);
if ((error) || (WEXITSTATUS(error))) {
- printf("Failed to run copy command %d %d.\n", error, WEXITSTATUS(error));
+ printf("Failed to run copy command %d %d.\n", error,
+ WEXITSTATUS(error));
return -1;
}
@@ -1279,7 +1291,7 @@ int reload_test(void)
sleep(1);
- sprintf(command, "cp %s %s", infile, outfile);
+ snprintf(command, PATH_MAX * 3, "cp %s %s", infile, outfile);
INIOUT(printf("Copy file again with command '%s'\n", command));
error = system(command);
@@ -1389,7 +1401,8 @@ int get_test(void)
}
srcdir = getenv("srcdir");
- sprintf(infile, "%s/ini/ini.d/real.conf", (srcdir == NULL) ? "." : srcdir);
+ snprintf(infile, PATH_MAX, "%s/ini/ini.d/real.conf",
+ (srcdir == NULL) ? "." : srcdir);
INIOUT(printf("Reading file %s\n", infile));
diff --git a/ini/ini_valueobj_ut.c b/ini/ini_valueobj_ut.c
index eb7937c..7911860 100644
--- a/ini/ini_valueobj_ut.c
+++ b/ini/ini_valueobj_ut.c
@@ -30,6 +30,8 @@
#define TRACE_HOME
#include "trace.h"
+#define TEST_SIZE 80
+
int verbose = 0;
#define VOOUT(foo) \
@@ -47,12 +49,12 @@ static int create_comment(int i, struct ini_comment **ic)
{
int error = EOK;
const char *template = ";Line 0 of the value %d";
- char comment[80];
+ char comment[TEST_SIZE];
struct ini_comment *new_ic = NULL;
TRACE_FLOW_ENTRY();
- sprintf(comment, template, i);
+ snprintf(comment, TEST_SIZE, template, i);
if ((error = ini_comment_create(&new_ic)) ||
@@ -579,7 +581,7 @@ int vo_copy_test(void)
}
/* Replace comment in the value */
- sprintf(comment, ";This is value with boundary %d", wrap);
+ snprintf(comment, TEST_SIZE, ";This is value with boundary %d", wrap);
VOOUT(printf("Comment: %s\n", comment));
error = ini_comment_replace(ic, 1, comment);
if (error) {