mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-24 22:55:23 +00:00
aa-helper: use g_autofree in create_profile
'template' might be used uninitialized. Use g_autofree for everything and remove all the custom labels. Signed-off-by: Ján Tomko <jtomko@redhat.com> Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
parent
6ffb444186
commit
4a10db14bb
@ -264,22 +264,21 @@ static int
|
|||||||
create_profile(const char *profile, const char *profile_name,
|
create_profile(const char *profile, const char *profile_name,
|
||||||
const char *profile_files, int virtType)
|
const char *profile_files, int virtType)
|
||||||
{
|
{
|
||||||
char *template;
|
g_autofree char *template = NULL;
|
||||||
char *tcontent = NULL;
|
g_autofree char *tcontent = NULL;
|
||||||
char *pcontent = NULL;
|
g_autofree char *pcontent = NULL;
|
||||||
char *replace_name = NULL;
|
g_autofree char *replace_name = NULL;
|
||||||
char *replace_files = NULL;
|
g_autofree char *replace_files = NULL;
|
||||||
char *tmp = NULL;
|
char *tmp = NULL;
|
||||||
const char *template_name = "\nprofile LIBVIRT_TEMPLATE";
|
const char *template_name = "\nprofile LIBVIRT_TEMPLATE";
|
||||||
const char *template_end = "\n}";
|
const char *template_end = "\n}";
|
||||||
int tlen, plen;
|
int tlen, plen;
|
||||||
int fd;
|
int fd;
|
||||||
int rc = -1;
|
|
||||||
const char *driver_name = NULL;
|
const char *driver_name = NULL;
|
||||||
|
|
||||||
if (virFileExists(profile)) {
|
if (virFileExists(profile)) {
|
||||||
vah_error(NULL, 0, _("profile exists"));
|
vah_error(NULL, 0, _("profile exists"));
|
||||||
goto end;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (virtType) {
|
switch (virtType) {
|
||||||
@ -296,22 +295,22 @@ create_profile(const char *profile, const char *profile_name,
|
|||||||
|
|
||||||
if (!virFileExists(template)) {
|
if (!virFileExists(template)) {
|
||||||
vah_error(NULL, 0, _("template does not exist"));
|
vah_error(NULL, 0, _("template does not exist"));
|
||||||
goto end;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((tlen = virFileReadAll(template, MAX_FILE_LEN, &tcontent)) < 0) {
|
if ((tlen = virFileReadAll(template, MAX_FILE_LEN, &tcontent)) < 0) {
|
||||||
vah_error(NULL, 0, _("failed to read AppArmor template"));
|
vah_error(NULL, 0, _("failed to read AppArmor template"));
|
||||||
goto end;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strstr(tcontent, template_name) == NULL) {
|
if (strstr(tcontent, template_name) == NULL) {
|
||||||
vah_error(NULL, 0, _("no replacement string in template"));
|
vah_error(NULL, 0, _("no replacement string in template"));
|
||||||
goto clean_tcontent;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strstr(tcontent, template_end) == NULL) {
|
if (strstr(tcontent, template_end) == NULL) {
|
||||||
vah_error(NULL, 0, _("no replacement string in template"));
|
vah_error(NULL, 0, _("no replacement string in template"));
|
||||||
goto clean_tcontent;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* '\nprofile <profile_name>\0' */
|
/* '\nprofile <profile_name>\0' */
|
||||||
@ -328,15 +327,15 @@ create_profile(const char *profile, const char *profile_name,
|
|||||||
|
|
||||||
if (plen > MAX_FILE_LEN || plen < tlen) {
|
if (plen > MAX_FILE_LEN || plen < tlen) {
|
||||||
vah_error(NULL, 0, _("invalid length for new profile"));
|
vah_error(NULL, 0, _("invalid length for new profile"));
|
||||||
goto clean_replace;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(pcontent = virStringReplace(tcontent, template_name, replace_name)))
|
if (!(pcontent = virStringReplace(tcontent, template_name, replace_name)))
|
||||||
goto clean_all;
|
return -1;
|
||||||
|
|
||||||
if (virtType != VIR_DOMAIN_VIRT_LXC) {
|
if (virtType != VIR_DOMAIN_VIRT_LXC) {
|
||||||
if (!(tmp = virStringReplace(pcontent, template_end, replace_files)))
|
if (!(tmp = virStringReplace(pcontent, template_end, replace_files)))
|
||||||
goto clean_all;
|
return -1;
|
||||||
VIR_FREE(pcontent);
|
VIR_FREE(pcontent);
|
||||||
pcontent = g_steal_pointer(&tmp);
|
pcontent = g_steal_pointer(&tmp);
|
||||||
}
|
}
|
||||||
@ -344,31 +343,21 @@ create_profile(const char *profile, const char *profile_name,
|
|||||||
/* write the file */
|
/* write the file */
|
||||||
if ((fd = open(profile, O_CREAT | O_EXCL | O_WRONLY, 0644)) == -1) {
|
if ((fd = open(profile, O_CREAT | O_EXCL | O_WRONLY, 0644)) == -1) {
|
||||||
vah_error(NULL, 0, _("failed to create profile"));
|
vah_error(NULL, 0, _("failed to create profile"));
|
||||||
goto clean_all;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (safewrite(fd, pcontent, plen - 1) < 0) { /* don't write the '\0' */
|
if (safewrite(fd, pcontent, plen - 1) < 0) { /* don't write the '\0' */
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
vah_error(NULL, 0, _("failed to write to profile"));
|
vah_error(NULL, 0, _("failed to write to profile"));
|
||||||
goto clean_all;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (VIR_CLOSE(fd) != 0) {
|
if (VIR_CLOSE(fd) != 0) {
|
||||||
vah_error(NULL, 0, _("failed to close or write to profile"));
|
vah_error(NULL, 0, _("failed to close or write to profile"));
|
||||||
goto clean_all;
|
return -1;
|
||||||
}
|
}
|
||||||
rc = 0;
|
|
||||||
|
|
||||||
clean_all:
|
return 0;
|
||||||
VIR_FREE(pcontent);
|
|
||||||
clean_replace:
|
|
||||||
VIR_FREE(replace_name);
|
|
||||||
VIR_FREE(replace_files);
|
|
||||||
clean_tcontent:
|
|
||||||
VIR_FREE(tcontent);
|
|
||||||
end:
|
|
||||||
VIR_FREE(template);
|
|
||||||
return rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user