Skip to content

Commit

Permalink
Avoid using sprintf (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
tklengyel committed Aug 15, 2018
1 parent eb7d2c2 commit 3efb0e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 67 deletions.
66 changes: 19 additions & 47 deletions libvmi/core.c
Expand Up @@ -78,81 +78,53 @@ open_config_file(
)
{
FILE *f = NULL;
char *location;
gchar *location;
char *sudo_user = NULL;
struct passwd *pw_entry = NULL;
char cwd[1024] = { 0 };

/* check current directory */
if (getcwd(cwd, sizeof(cwd)) != NULL) {
location = g_malloc0(snprintf(NULL,0,"%s/libvmi.conf", cwd)+1);
if ( !location )
return NULL;

sprintf(location, "%s/libvmi.conf", cwd);
location = g_strconcat(cwd, "/libvmi.conf", NULL);
dbprint(VMI_DEBUG_CORE, "--looking for config file at %s\n", location);

f = fopen(location, "r");

if (f) {
goto success;
}
g_free(location);

if (f)
return f;
}

/* next check home directory of sudo user */
if ((sudo_user = getenv("SUDO_USER")) != NULL) {
if ((pw_entry = getpwnam(sudo_user)) != NULL) {
location = g_malloc0(snprintf(NULL,0,"%s/etc/libvmi.conf", pw_entry->pw_dir)+1);
if ( !location )
return NULL;

sprintf(location, "%s/etc/libvmi.conf",
pw_entry->pw_dir);
dbprint(VMI_DEBUG_CORE, "--looking for config file at %s\n", location);
if ((sudo_user = getenv("SUDO_USER")) != NULL && (pw_entry = getpwnam(sudo_user)) != NULL) {
location = g_strconcat(pw_entry->pw_dir, "/etc/libvmi.conf", NULL);
dbprint(VMI_DEBUG_CORE, "--looking for config file at %s\n", location);

f = fopen(location, "r");
f = fopen(location, "r");
g_free(location);

if (f) {
goto success;
}
g_free(location);
}
if (f)
return f;
}

/* next check home directory for current user */
location = g_malloc0(snprintf(NULL,0,"%s/etc/libvmi.conf", getenv("HOME"))+1);
if ( !location )
return NULL;

sprintf(location, "%s/etc/libvmi.conf", getenv("HOME"));
location = g_strconcat(getenv("HOME"), "/etc/libvmi.conf", NULL);
dbprint(VMI_DEBUG_CORE, "--looking for config file at %s\n", location);

f = fopen(location, "r");

if (f) {
goto success;
}
g_free(location);

if (f)
return f;

/* finally check in /etc */
dbprint(VMI_DEBUG_CORE, "--looking for config file at /etc/libvmi.conf\n");
location = g_malloc0(strlen("/etc/libvmi.conf")+1);
if ( !location )
return NULL;
f = fopen("/etc/libvmi.conf", "r");

sprintf(location, "/etc/libvmi.conf");
f = fopen(location, "r");
if (f) {
goto success;
}
g_free(location);
if (f)
return f;

return NULL;
success:
dbprint(VMI_DEBUG_CORE, "**Using config file at %s\n", location);
free(location);
return f;
}

static status_t
Expand Down
6 changes: 3 additions & 3 deletions libvmi/driver/xen/libxc_wrapper.c
Expand Up @@ -145,14 +145,14 @@ status_t create_libxc_wrapper(xen_instance_t *xen)
wrapper->handle = dlopen ("libxenctrl.so", RTLD_NOW | RTLD_GLOBAL);

if ( !wrapper->handle ) {
char *alternate = g_malloc0(snprintf(NULL, 0, "libxenctrl.so.%u.%u",
xen->major_version, xen->minor_version)+1);
sprintf(alternate, "libxenctrl.so.%u.%u", xen->major_version, xen->minor_version);
gchar *tmp = g_strdup_printf("%u.%u", xen->major_version, xen->minor_version);
gchar *alternate = g_strconcat("libxenctrl.so.", tmp, NULL);

dbprint(VMI_DEBUG_XEN, "--libxc_wrapper looking for %s\n", alternate);

wrapper->handle = dlopen (alternate, RTLD_NOW | RTLD_GLOBAL);
g_free(alternate);
g_free(tmp);

if ( !wrapper->handle ) {
fprintf(stderr, "Failed to find a suitable libxenctrl.so at any of the standard paths!\n");
Expand Down
29 changes: 12 additions & 17 deletions libvmi/driver/xen/xen.c
Expand Up @@ -194,7 +194,6 @@ uint64_t xen_get_domainid_from_name(
unsigned int size = 0, i = 0;
xs_transaction_t xth = XBT_NULL;
uint64_t domainid = VMI_INVALID_DOMID;
char *tmp;

struct xs_handle *xsh = xen->libxsw.xs_open(0);

Expand All @@ -205,11 +204,9 @@ uint64_t xen_get_domainid_from_name(
for (i = 0; i < size; ++i) {
/* read in name */
char *idStr = domains[i];

tmp = g_malloc0(snprintf(NULL, 0, "/local/domain/%s/name", idStr)+1);
sprintf(tmp, "/local/domain/%s/name", idStr);
char *tmp = g_strconcat("/local/domain/", idStr, "/name", NULL);
char *nameCandidate = xen->libxsw.xs_read(xsh, xth, tmp, NULL);
free(tmp);
g_free(tmp);

// if name matches, then return number
if (nameCandidate != NULL &&
Expand Down Expand Up @@ -264,10 +261,11 @@ status_t xen_get_name_from_domainid(
if (!xsh)
goto _bail;

char *tmp = g_malloc0(snprintf(NULL, 0, "/local/domain/%"PRIu64"/name", domainid)+1);
sprintf(tmp, "/local/domain/%"PRIu64"/name", domainid);
char *nameCandidate = xen->libxsw.xs_read(xsh, xth, tmp, NULL);
free(tmp);
gchar *tmp = g_strdup_printf("%"PRIu64, domainid);
gchar *tmp2 = g_strconcat("/local/domain/", tmp, "/name", NULL);
char *nameCandidate = xen->libxsw.xs_read(xsh, xth, tmp2, NULL);
g_free(tmp);
g_free(tmp2);

if (nameCandidate != NULL) {
*name = nameCandidate;
Expand Down Expand Up @@ -571,14 +569,11 @@ xen_get_domainname(
goto _bail;
}

char *tmp = g_malloc0(snprintf(NULL,
0,
"/local/domain/%"PRIu64"/name",
xen->domainid)
+1);
sprintf(tmp, "/local/domain/%"PRIu64"/name", xen->domainid);
*name = xen->libxsw.xs_read(xen->xshandle, xth, tmp, NULL);
free(tmp);
gchar *tmp = g_strdup_printf("%"PRIu64, xen->domainid);
gchar *tmp2 = g_strconcat("/local/domain/", tmp, "/name", NULL);
*name = xen->libxsw.xs_read(xen->xshandle, xth, tmp2, NULL);
g_free(tmp);
g_free(tmp2);

if (*name == NULL) {
errprint("Couldn't get name of domain %"PRIu64" from Xenstore\n", xen->domainid);
Expand Down

0 comments on commit 3efb0e8

Please sign in to comment.