Skip to content

Commit

Permalink
Replacing atoi with proper number parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneTR committed Sep 28, 2024
1 parent 209e1cb commit 93b8806
Show file tree
Hide file tree
Showing 39 changed files with 116 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ venv/
lib/hardware_info_root.py
tools/cluster/cleanup.sh
node_modules/
lib/c/parse_int.o
6 changes: 3 additions & 3 deletions install_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ fi
sudo systemctl stop tinyproxy
sudo systemctl disable tinyproxy

build_binaries

print_message "Building C libs"
#make -C "lib/c"
make -C "lib/c"

build_binaries

print_message "Building sgx binaries"
make -C lib/sgx-software-enable
Expand Down
4 changes: 4 additions & 0 deletions lib/c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CFLAGS = -o3 -Wall

parse_int.o: parse_int.c
gcc -c $< $(CFLAGS) -o $@
32 changes: 32 additions & 0 deletions lib/c/parse_int.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "parse_int.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <limits.h>

unsigned int parse_int(char *argument) {
unsigned long int number = 0;
char *endptr;

errno = 0; // Reset errno before the call
number = strtoul(argument, &endptr, 10);

if (errno == ERANGE && (number == LONG_MAX || number == LONG_MIN)) {
fprintf(stderr, "Error: Could not parse -i argument - Number out of range\n");
exit(1);
} else if (errno != 0 && number == 0) {
fprintf(stderr, "Error: Could not parse -i argument - Invalid number\n");
exit(1);
} else if (endptr == argument) {
fprintf(stderr, "Error: Could not parse -i argument - No digits were found\n");
exit(1);
} else if (*endptr != '\0') {
fprintf(stderr, "Error: Could not parse -i argument - Invalid characters after number\n");
exit(1);
}

return number;
}

6 changes: 6 additions & 0 deletions lib/c/parse_int.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef PARSE_INT_H
#define PARSE_INT_H

unsigned int parse_int(char *argument);

#endif // PARSE_INT_H
4 changes: 2 additions & 2 deletions metric_providers/cpu/energy/rapl/msr/component/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CFLAGS = -o3 -Wall -lm
CFLAGS = -o3 -Wall -lm -I../../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
sudo chown root $@
sudo chmod u+s $@
4 changes: 2 additions & 2 deletions metric_providers/cpu/energy/rapl/msr/component/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <sys/syscall.h>
#include <sys/time.h>
#include <limits.h>

#include "parse_int.h"

/* AMD Support */
#define MSR_AMD_RAPL_POWER_UNIT 0xc0010299
Expand Down Expand Up @@ -521,7 +521,7 @@ int main(int argc, char **argv) {
printf("\t-c : check system and exit\n");
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'd':
measurement_mode=MEASURE_DRAM;
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/cpu/time/cgroup/container/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
3 changes: 2 additions & 1 deletion metric_providers/cpu/time/cgroup/container/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h> // for strtok
#include <getopt.h>
#include <limits.h>
#include "parse_int.h"

#define DOCKER_CONTAINER_ID_BUFFER 65 // Docker container ID size is 64 + 1 byte for NUL termination

Expand Down Expand Up @@ -156,7 +157,7 @@ int main(int argc, char **argv) {
printf("\tCLOCKS_PER_SEC\t%ld\n", CLOCKS_PER_SEC);
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'r':
rootless_mode = 1;
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/cpu/time/cgroup/system/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
3 changes: 2 additions & 1 deletion metric_providers/cpu/time/cgroup/system/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include "parse_int.h"

// All variables are made static, because we believe that this will
// keep them local in scope to the file and not make them persist in state
Expand Down Expand Up @@ -77,7 +78,7 @@ int main(int argc, char **argv) {
printf("\tCLOCKS_PER_SEC\t%ld\n", CLOCKS_PER_SEC);
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'c':
check_system_flag = 1;
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/cpu/time/procfs/system/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
4 changes: 2 additions & 2 deletions metric_providers/cpu/time/procfs/system/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <unistd.h>
#include <sys/time.h>
#include <time.h>

#include "parse_int.h"

// All variables are made static, because we believe that this will
// keep them local in scope to the file and not make them persist in state
Expand Down Expand Up @@ -86,7 +86,7 @@ int main(int argc, char **argv) {
printf("\tCLOCKS_PER_SEC\t%ld\n", CLOCKS_PER_SEC);
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'c':
check_system_flag = 1;
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/cpu/utilization/cgroup/container/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
3 changes: 2 additions & 1 deletion metric_providers/cpu/utilization/cgroup/container/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h> // for strtok
#include <getopt.h>
#include <limits.h>
#include "parse_int.h"

#define DOCKER_CONTAINER_ID_BUFFER 65 // Docker container ID size is 64 + 1 byte for NUL termination

Expand Down Expand Up @@ -248,7 +249,7 @@ int main(int argc, char **argv) {
printf("\tCLOCKS_PER_SEC\t%ld\n", CLOCKS_PER_SEC);
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'r':
rootless_mode = 1;
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/cpu/utilization/mach/system/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
3 changes: 2 additions & 1 deletion metric_providers/cpu/utilization/mach/system/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mach/mach.h>
#include <mach/mach_host.h>
#include <unistd.h>
#include "parse_int.h"

void loop_utilization(unsigned int msleep_time) {
processor_info_array_t cpuInfo = NULL, prevCpuInfo = NULL;
Expand Down Expand Up @@ -92,7 +93,7 @@ int main(int argc, char **argv) {
printf("\t-c : check system and exit\n\n");
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
if (msleep_time < 50){
fprintf(stderr,"A value of %i is to small. Results will include 0s as the kernel does not update as fast.\n",msleep_time);
}
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/cpu/utilization/procfs/system/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
3 changes: 2 additions & 1 deletion metric_providers/cpu/utilization/procfs/system/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include "parse_int.h"

typedef struct procfs_time_t { // struct is a specification and this static makes no sense here
unsigned long user_time;
Expand Down Expand Up @@ -118,7 +119,7 @@ int main(int argc, char **argv) {
printf("\tCLOCKS_PER_SEC\t%ld\n", CLOCKS_PER_SEC);
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'c':
check_system_flag = 1;
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/disk/io/cgroup/container/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
3 changes: 2 additions & 1 deletion metric_providers/disk/io/cgroup/container/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string.h> // for strtok
#include <getopt.h>
#include <limits.h>
#include "parse_int.h"

#define DOCKER_CONTAINER_ID_BUFFER 65 // Docker container ID size is 64 + 1 byte for NUL termination

Expand Down Expand Up @@ -166,7 +167,7 @@ int main(int argc, char **argv) {
printf("\t-c : check system and exit\n\n");
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'r':
rootless_mode = 1;
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/disk/io/procfs/system/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
3 changes: 2 additions & 1 deletion metric_providers/disk/io/procfs/system/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <limits.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include "parse_int.h"

// All variables are made static, because we believe that this will
// keep them local in scope to the file and not make them persist in state
Expand Down Expand Up @@ -92,7 +93,7 @@ int main(int argc, char **argv) {
printf("\t-c : check system and exit\n\n");
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'c':
check_system_flag = 1;
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/disk/used/statvfs/system/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
3 changes: 2 additions & 1 deletion metric_providers/disk/used/statvfs/system/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unistd.h>
#include <sys/time.h>
#include <getopt.h>
#include "parse_int.h"

// All variables are made static, because we believe that this will
// keep them local in scope to the file and not make them persist in state
Expand Down Expand Up @@ -73,7 +74,7 @@ int main(int argc, char **argv) {
printf("\t-c : check system and exit\n\n");
exit(0);
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'c':
check_system_flag = 1;
Expand Down
6 changes: 3 additions & 3 deletions metric_providers/lm_sensors/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ PROGRAM = metric-provider-binary
SOURCES = source.c chips.c
GLIBLIB = $(shell pkg-config --libs glib-2.0)
GLIBGLAGS = $(shell pkg-config --cflags glib-2.0)
CFLAGS = -o3 -Wall -Llib -lsensors $(GLIBLIB) $(GLIBGLAGS)
CFLAGS = -o3 -Wall -Llib -lsensors $(GLIBLIB) $(GLIBGLAGS) -I../../lib/c

binary:
gcc $(SOURCES) $(CFLAGS) -o $(PROGRAM)
gcc ../../lib/c/parse_int.o $(SOURCES) $(CFLAGS) -o $(PROGRAM)

lint:
cpplint *.c *.h

debug:
gcc $(SOURCES) $(CFLAGS) -g -o $(PROGRAM)
gcc ../../lib/c/parse_int.o $(SOURCES) $(CFLAGS) -g -o $(PROGRAM)
5 changes: 3 additions & 2 deletions metric_providers/lm_sensors/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "sensors/error.h"
#include "sensors/sensors.h"
#include "source.h"
#include "parse_int.h"

int fahrenheit;
char degstr[5]; /* store the correct string to print degrees */
Expand Down Expand Up @@ -235,10 +236,10 @@ int main(int argc, char *argv[]) {
fahrenheit = 1;
break;
case 'i':
msleep_time = atoi(optarg);
msleep_time = parse_int(optarg);
break;
case 'n':
measurement_amount = atoi(optarg);
measurement_amount = parse_int(optarg);
break;
default:
exit(1);
Expand Down
4 changes: 2 additions & 2 deletions metric_providers/memory/energy/rapl/msr/component/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CFLAGS = -o3 -Wall -lm
CFLAGS = -o3 -Wall -lm -I../../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
sudo chown root $@
sudo chmod u+s $@
4 changes: 2 additions & 2 deletions metric_providers/memory/used/cgroup/container/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CFLAGS = -o3 -Wall
CFLAGS = -o3 -Wall -I../../../../../lib/c

metric-provider-binary: source.c
gcc $< $(CFLAGS) -o $@
gcc ../../../../../lib/c/parse_int.o $< $(CFLAGS) -o $@
Loading

0 comments on commit 93b8806

Please sign in to comment.