Resolve some Coverity scan misfires in PCP platform code

This commit is contained in:
Nathan Scott 2021-04-08 13:15:59 +10:00
parent da454997bf
commit b232119e4b
2 changed files with 32 additions and 38 deletions

View File

@ -20,7 +20,7 @@ int main(int argc, char** argv) {
/* extract environment variables */ /* extract environment variables */
opts.flags |= PM_OPTFLAG_ENV_ONLY; opts.flags |= PM_OPTFLAG_ENV_ONLY;
pmGetOptions(argc, argv, &opts); (void)pmGetOptions(argc, argv, &opts);
return CommandLine_run(name, argc, argv); return CommandLine_run(name, argc, argv);
} }

View File

@ -422,8 +422,7 @@ void Platform_init(void) {
exit(1); exit(1);
} }
/* setup timezones and other general startup preparation completion */ /* setup timezones and other general startup preparation completion */
pmGetContextOptions(sts, &opts); if (pmGetContextOptions(sts, &opts) < 0 || opts.errors) {
if (opts.errors) {
pmflush(); pmflush();
exit(1); exit(1);
} }
@ -688,51 +687,46 @@ void Platform_getRelease(char** string) {
} }
/* first call, extract just-sampled values */ /* first call, extract just-sampled values */
pmAtomValue value; pmAtomValue sysname, release, machine, distro;
if (!Metric_values(PCP_UNAME_SYSNAME, &sysname, 1, PM_TYPE_STRING))
char* name = NULL; sysname.cp = NULL;
if (Metric_values(PCP_UNAME_SYSNAME, &value, 1, PM_TYPE_STRING)) if (!Metric_values(PCP_UNAME_RELEASE, &release, 1, PM_TYPE_STRING))
name = value.cp; release.cp = NULL;
char* release = NULL; if (!Metric_values(PCP_UNAME_MACHINE, &machine, 1, PM_TYPE_STRING))
if (Metric_values(PCP_UNAME_RELEASE, &value, 1, PM_TYPE_STRING)) machine.cp = NULL;
release = value.cp; if (!Metric_values(PCP_UNAME_DISTRO, &distro, 1, PM_TYPE_STRING))
char* machine = NULL; distro.cp = NULL;
if (Metric_values(PCP_UNAME_MACHINE, &value, 1, PM_TYPE_STRING))
machine = value.cp;
char* distro = NULL;
if (Metric_values(PCP_UNAME_DISTRO, &value, 1, PM_TYPE_STRING))
distro = value.cp;
size_t length = 16; /* padded for formatting characters */ size_t length = 16; /* padded for formatting characters */
if (name) if (sysname.cp)
length += strlen(name); length += strlen(sysname.cp);
if (release) if (release.cp)
length += strlen(release); length += strlen(release.cp);
if (machine) if (machine.cp)
length += strlen(machine); length += strlen(machine.cp);
if (distro) if (distro.cp)
length += strlen(distro); length += strlen(distro.cp);
pcp->release = xCalloc(1, length); pcp->release = xCalloc(1, length);
if (name) { if (sysname.cp) {
strcat(pcp->release, name); strcat(pcp->release, sysname.cp);
strcat(pcp->release, " "); strcat(pcp->release, " ");
} }
if (release) { if (release.cp) {
strcat(pcp->release, release); strcat(pcp->release, release.cp);
strcat(pcp->release, " "); strcat(pcp->release, " ");
} }
if (machine) { if (machine.cp) {
strcat(pcp->release, "["); strcat(pcp->release, "[");
strcat(pcp->release, machine); strcat(pcp->release, machine.cp);
strcat(pcp->release, "] "); strcat(pcp->release, "] ");
} }
if (distro) { if (distro.cp) {
if (pcp->release[0] != '\0') { if (pcp->release[0] != '\0') {
strcat(pcp->release, "@ "); strcat(pcp->release, "@ ");
strcat(pcp->release, distro); strcat(pcp->release, distro.cp);
} else { } else {
strcat(pcp->release, distro); strcat(pcp->release, distro.cp);
} }
strcat(pcp->release, " "); strcat(pcp->release, " ");
} }
@ -740,10 +734,10 @@ void Platform_getRelease(char** string) {
if (pcp->release) /* cull trailing space */ if (pcp->release) /* cull trailing space */
pcp->release[strlen(pcp->release)] = '\0'; pcp->release[strlen(pcp->release)] = '\0';
free(distro); free(distro.cp);
free(machine); free(machine.cp);
free(release); free(release.cp);
free(name); free(sysname.cp);
} }
char* Platform_getProcessEnv(pid_t pid) { char* Platform_getProcessEnv(pid_t pid) {