summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorMaciej Suminski <maciej.suminski@cern.ch>2017-08-09 11:46:41 +0200
committerNico Huber <nico.h@gmx.de>2017-08-22 10:10:34 +0000
commitd5fb99eab679a5b66b64665575ff40b84d01c3b9 (patch)
treeea567e2ec7ef69cb5e585bd0f4de2320e146687c /util
parentb3dfcb863cdc62cd2cb65e97e0043311b151c558 (diff)
util/inteltool: Remove duplicated error message, switch to snprintf
Passing a string containing output from strerror() to perror() causes double error message display. It is also causing segfaults when the error message is longer than temp_string capacity. To fix the problems, sterror() call has been removed so the error message is printed only once. This could be enough to avoid segfaults, but it is a good practice to limit output size with snprintf(). Change-Id: I5ccc37e404f278cafae0a451c5acaa27d7907cce Signed-off-by: Maciej Suminski <maciej.suminski@cern.ch> Reviewed-on: https://review.coreboot.org/21025 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Nico Huber <nico.h@gmx.de>
Diffstat (limited to 'util')
-rw-r--r--util/inteltool/cpu.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/util/inteltool/cpu.c b/util/inteltool/cpu.c
index 7b2e503757..360b86b9d0 100644
--- a/util/inteltool/cpu.c
+++ b/util/inteltool/cpu.c
@@ -117,14 +117,13 @@ static int open_and_seek(int cpu, unsigned long msr, int mode, int *fd)
*fd = open(dev, mode);
if (*fd < 0) {
- sprintf(temp_string,
- "open(\"%s\"): %s\n", dev, strerror(errno));
+ snprintf(temp_string, sizeof(temp_string), "open(\"%s\")", dev);
perror(temp_string);
return -1;
}
if (lseek(*fd, msr, SEEK_SET) == (off_t)-1) {
- sprintf(temp_string, "lseek(%lu): %s\n", msr, strerror(errno));
+ snprintf(temp_string, sizeof(temp_string), "lseek(%lu)", msr);
perror(temp_string);
close(*fd);
return -1;
@@ -141,7 +140,8 @@ msr_t rdmsr_from_cpu(int cpu, unsigned long addr)
char temp_string[50];
if (open_and_seek(cpu, addr, O_RDONLY, &fd) < 0) {
- sprintf(temp_string, "Could not read MSR for CPU#%d", cpu);
+ snprintf(temp_string, sizeof(temp_string),
+ "Could not read MSR for CPU#%d", cpu);
perror(temp_string);
}
@@ -194,13 +194,11 @@ int print_sgx(void)
#ifndef __DARWIN__
int ncpus = get_number_of_cpus();
int i = 0;
- char temp_string[50];
printf("\n============= Dumping INTEL SGX status =============");
if (ncpus < 1) {
- sprintf(temp_string, "Failed to get number of CPUs\n");
- perror(temp_string);
+ perror("Failed to get number of CPUs");
error = -1;
} else {
printf("\nNumber of CPUs = %d\n", ncpus);