Browse Source

Use snprintf() instead sprintf()

Serj Kalichev 10 years ago
parent
commit
64fe6c809d
7 changed files with 38 additions and 12 deletions
  1. 4 1
      balance.c
  2. 1 0
      birq.c
  3. 10 4
      cpu.c
  4. 14 4
      irq.c
  5. 7 3
      numa.c
  6. 1 0
      pxm.c
  7. 1 0
      statistics.c

+ 4 - 1
balance.c

@@ -126,10 +126,13 @@ static int irq_set_affinity(irq_t *irq, cpumask_t *cpumask)
 	if (!irq)
 		return -1;
 
-	sprintf(path, "%s/%u/smp_affinity", PROC_IRQ, irq->irq);
+	snprintf(path, sizeof(path),
+		"%s/%u/smp_affinity", PROC_IRQ, irq->irq);
+	path[sizeof(path) - 1] = '\0';
 	if (!(fd = fopen(path, "w")))
 		return -1;
 	cpumask_scnprintf(buf, sizeof(buf), *cpumask);
+	buf[sizeof(buf) - 1] = '\0';
 	if ((fprintf(fd, "%s\n", buf) < 0) || (fflush(fd) == EOF)) {
 		/* The affinity for some IRQ can't be changed. So don't
 		   consider such IRQs. The example is IRQ 0 - timer.

+ 1 - 0
birq.c

@@ -110,6 +110,7 @@ int main(int argc, char **argv)
 		} else {
 			char str[20];
 			snprintf(str, sizeof(str), "%u\n", getpid());
+			str[sizeof(str) - 1] = '\0';
 			if (write(pidfd, str, strlen(str)) < 0)
 				syslog(LOG_WARNING, "Can't write to %s: %s",
 					opts->pidfile, strerror(errno));

+ 10 - 4
cpu.c

@@ -134,6 +134,7 @@ static void show_cpu_info(cpu_t *cpu)
 {
 	char buf[NR_CPUS + 1];
 	cpumask_scnprintf(buf, sizeof(buf), cpu->cpumask);
+	buf[sizeof(buf) - 1] = '\0';
 	printf("CPU %d package %d core %d mask %s\n", cpu->id, cpu->package_id, cpu->core_id, buf);
 }
 
@@ -164,13 +165,16 @@ int scan_cpus(lub_list_t *cpus, int ht)
 	cpumask_t thread_siblings;
 
 	for (id = 0; id < NR_CPUS; id++) {
-		sprintf(path, "%s/cpu%d", SYSFS_CPU_PATH, id);
+		snprintf(path, sizeof(path), "%s/cpu%d", SYSFS_CPU_PATH, id);
+		path[sizeof(path) - 1] = '\0';
 		if (access(path, F_OK))
 			break;
 
 		/* Try to get package_id */
-		sprintf(path, "%s/cpu%d/topology/physical_package_id",
+		snprintf(path, sizeof(path),
+			"%s/cpu%d/topology/physical_package_id",
 			SYSFS_CPU_PATH, id);
+		path[sizeof(path) - 1] = '\0';
 		if (!(fd = fopen(path, "r")))
 			continue;
 		if (fscanf(fd, "%u", &package_id) < 0) {
@@ -180,8 +184,9 @@ int scan_cpus(lub_list_t *cpus, int ht)
 		fclose(fd);
 
 		/* Try to get core_id */
-		sprintf(path, "%s/cpu%d/topology/core_id",
+		snprintf(path, sizeof(path), "%s/cpu%d/topology/core_id",
 			SYSFS_CPU_PATH, id);
+		path[sizeof(path) - 1] = '\0';
 		if (!(fd = fopen(path, "r")))
 			continue;
 		if (fscanf(fd, "%u", &core_id) < 0) {
@@ -193,8 +198,9 @@ int scan_cpus(lub_list_t *cpus, int ht)
 		/* Get thread siblings */
 		cpus_clear(thread_siblings);
 		cpu_set(id, thread_siblings);
-		sprintf(path, "%s/cpu%d/topology/thread_siblings",
+		snprintf(path, sizeof(path), "%s/cpu%d/topology/thread_siblings",
 			SYSFS_CPU_PATH, id);
+		path[sizeof(path) - 1] = '\0';
 		if ((fd = fopen(path, "r"))) {
 			if (getline(&str, &sz, fd) >= 0)
 				cpumask_parse_user(str, strlen(str), thread_siblings);

+ 14 - 4
irq.c

@@ -98,10 +98,12 @@ int irq_list_free(lub_list_t *irqs)
 static void irq_show(irq_t *irq)
 {
 	char buf[NR_CPUS + 1];
+
 	if (cpus_full(irq->local_cpus))
 		snprintf(buf, sizeof(buf), "*");
 	else
 		cpumask_scnprintf(buf, sizeof(buf), irq->local_cpus);
+	buf[sizeof(buf) - 1] = '\0';
 	printf("IRQ %3d %s [%s] %s\n", irq->irq, buf, STR(irq->type), STR(irq->desc));
 }
 
@@ -139,7 +141,9 @@ static int parse_local_cpus(lub_list_t *irqs, const char *sysfs_path,
 		return 0;
 	}
 
-	sprintf(path, "%s/%s/local_cpus", SYSFS_PCI_PATH, sysfs_path);
+	snprintf(path, sizeof(path),
+		"%s/%s/local_cpus", SYSFS_PCI_PATH, sysfs_path);
+	path[sizeof(path) - 1] = '\0';
 	if (!(fd = fopen(path, "r")))
 		return -1;
 	if (getline(&str, &sz, fd) < 0) {
@@ -175,7 +179,9 @@ static int parse_sysfs(lub_list_t *irqs, lub_list_t *pxms)
 			continue;
 
 		/* Search for MSI IRQs. Since linux-3.2 */
-		sprintf(path, "%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
+		snprintf(path, sizeof(path),
+			"%s/%s/msi_irqs", SYSFS_PCI_PATH, dent->d_name);
+		path[sizeof(path) - 1] = '\0';
 		if ((msi = opendir(path))) {
 			while((ment = readdir(msi))) {
 				if (!strcmp(ment->d_name, ".") ||
@@ -191,7 +197,9 @@ static int parse_sysfs(lub_list_t *irqs, lub_list_t *pxms)
 		}
 
 		/* Try to get IRQ number from irq file */
-		sprintf(path, "%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
+		snprintf(path, sizeof(path),
+			"%s/%s/irq", SYSFS_PCI_PATH, dent->d_name);
+		path[sizeof(path) - 1] = '\0';
 		if (!(fd = fopen(path, "r")))
 			continue;
 		if (fscanf(fd, "%d", &num) < 0) {
@@ -219,7 +227,9 @@ int irq_get_affinity(irq_t *irq)
 	if (!irq)
 		return -1;
 
-	sprintf(path, "%s/%u/smp_affinity", PROC_IRQ, irq->irq);
+	snprintf(path, sizeof(path),
+		"%s/%u/smp_affinity", PROC_IRQ, irq->irq);
+	path[sizeof(path) - 1] = '\0';
 	if (!(fd = fopen(path, "r")))
 		return -1;
 	if (getline(&str, &sz, fd) < 0) {

+ 7 - 3
numa.c

@@ -81,6 +81,7 @@ static void show_numa_info(numa_t *numa)
 {
 	char buf[NR_CPUS + 1];
 	cpumask_scnprintf(buf, sizeof(buf), numa->cpumap);
+	buf[sizeof(buf) - 1] = '\0';
 	printf("NUMA node %d cpumap %s\n", numa->id, buf);
 }
 
@@ -109,7 +110,9 @@ int scan_numas(lub_list_t *numas)
 	cpumask_t cpumap;
 
 	for (id = 0; id < NR_NUMA_NODES; id++) {
-		sprintf(path, "%s/node%d", SYSFS_NUMA_PATH, id);
+		snprintf(path, sizeof(path),
+			"%s/node%d", SYSFS_NUMA_PATH, id);
+		path[sizeof(path) - 1] = '\0';
 		if (access(path, F_OK))
 			break;
 
@@ -119,8 +122,9 @@ int scan_numas(lub_list_t *numas)
 		}
 
 		/* Get NUMA node cpumap */
-		sprintf(path, "%s/node%d/cpumap",
-			SYSFS_NUMA_PATH, id);
+		snprintf(path, sizeof(path),
+			"%s/node%d/cpumap", SYSFS_NUMA_PATH, id);
+		path[sizeof(path) - 1] = '\0';
 		if ((fd = fopen(path, "r"))) {
 			if (getline(&str, &sz, fd) >= 0)
 				cpumask_parse_user(str, strlen(str), cpumap);

+ 1 - 0
pxm.c

@@ -64,6 +64,7 @@ static void show_pxm_info(pxm_t *pxm)
 		snprintf(buf, sizeof(buf), "*");
 	else
 		cpumask_scnprintf(buf, sizeof(buf), pxm->cpumask);
+	buf[sizeof(buf) - 1] = '\0';
 	printf("PXM: %s cpumask %s\n", pxm->addr, buf);
 }
 

+ 1 - 0
statistics.c

@@ -141,6 +141,7 @@ void show_statistics(lub_list_t *cpus, int verbose)
 				snprintf(buf, sizeof(buf), "*");
 			else
 				cpumask_scnprintf(buf, sizeof(buf), irq->affinity);
+			buf[sizeof(buf) - 1] = '\0';
 			printf("    IRQ %3u, [%s], weight %d, intr %llu, %s\n", irq->irq, buf, irq->weight, irq->intr, irq->desc);
 		}
 	}