Skip to content

Commit 32b5845

Browse files
committed
Safe memory allocation for event storage
1 parent d74cb65 commit 32b5845

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/fping.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ extern int h_errno;
151151

152152
/* maxima and minima */
153153
#ifdef FPING_SAFE_LIMITS
154-
#define MIN_INTERVAL 1 /* in millisec */
155-
#define MIN_PERHOST_INTERVAL 10 /* in millisec */
154+
#define MIN_INTERVAL_MS 1 /* in millisec */
155+
#define MIN_PERHOST_INTERVAL_MS 10 /* in millisec */
156156
#else
157-
#define MIN_INTERVAL 0
158-
#define MIN_PERHOST_INTERVAL 0
157+
#define MIN_INTERVAL_MS 0
158+
#define MIN_PERHOST_INTERVAL_MS 0.001 /* (= in theory minimum 592 mbps on wire) */
159159
#endif
160160

161161
/* response time array flags */
@@ -1057,15 +1057,15 @@ int main(int argc, char **argv)
10571057
exit(1);
10581058
}
10591059

1060-
#ifdef FPING_SAFE_LIMITS
1061-
if ((interval < (int64_t)MIN_INTERVAL * 1000000 || perhost_interval < (int64_t)MIN_PERHOST_INTERVAL * 1000000)
1062-
&& getuid()) {
1063-
fprintf(stderr, "%s: these options are too risky for mere mortals.\n", prog);
1064-
fprintf(stderr, "%s: You need -i >= %u and -p >= %u\n",
1065-
prog, MIN_INTERVAL, MIN_PERHOST_INTERVAL);
1060+
if (interval < (float)MIN_INTERVAL_MS * 1000000 && getuid()) {
1061+
fprintf(stderr, "%s: -i must be >= %g\n", prog, (float)MIN_INTERVAL_MS);
1062+
exit(1);
1063+
}
1064+
1065+
if (perhost_interval < (float)MIN_PERHOST_INTERVAL_MS * 1000000 && getuid()) {
1066+
fprintf(stderr, "%s: -p must be >= %g\n", prog, (float)MIN_PERHOST_INTERVAL_MS);
10661067
exit(1);
10671068
}
1068-
#endif
10691069

10701070
if (ping_data_size > MAX_PING_DATA) {
10711071
fprintf(stderr, "%s: data size %u not valid, must not be larger than %u\n",
@@ -3448,7 +3448,13 @@ void add_addr(char *name, char *host, struct sockaddr *ipaddr, socklen_t ipaddr_
34483448

34493449
/* allocate event storage */
34503450
p->event_storage_ping = (struct event *)calloc(event_storage_count, sizeof(struct event));
3451+
if (!p->event_storage_ping) {
3452+
errno_crash_and_burn("can't allocate event_storage");
3453+
}
34513454
p->event_storage_timeout = (struct event *)calloc(event_storage_count, sizeof(struct event));
3455+
if (!p->event_storage_timeout) {
3456+
errno_crash_and_burn("can't allocate event_storage");
3457+
}
34523458

34533459
/* schedule first ping */
34543460
host_add_ping_event(p, 0, current_time_ns);

0 commit comments

Comments
 (0)