From 2d3a47d5386d11c6e6c141afd50bdb56e2b087ce Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sun, 27 Sep 2020 10:32:40 -0700 Subject: Clean up allocation of some lists. Always heck wehther the allocation succeeds, and fail if it doesn't. Set the count of elements of the list only if the list was successfully allocated. For stylistic consistency, also seet it after we've set all the elements of the list. --- pcap-airpcap.c | 2 +- pcap-bpf.c | 2 +- pcap-dag.c | 2 +- pcap-linux.c | 29 +++++++++++++++++++++-------- pcap-snf.c | 2 +- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/pcap-airpcap.c b/pcap-airpcap.c index 46b7dd51..703f23ac 100644 --- a/pcap-airpcap.c +++ b/pcap-airpcap.c @@ -895,10 +895,10 @@ airpcap_activate(pcap_t *p) p->dlt_list = (u_int *) malloc(sizeof(u_int) * 3); if (p->dlt_list == NULL) goto bad; - p->dlt_count = 3; p->dlt_list[0] = DLT_IEEE802_11_RADIO; p->dlt_list[1] = DLT_PPI; p->dlt_list[2] = DLT_IEEE802_11; + p->dlt_count = 3; p->read_op = airpcap_read; p->inject_op = airpcap_inject; diff --git a/pcap-bpf.c b/pcap-bpf.c index 10e3fab4..881ce196 100644 --- a/pcap-bpf.c +++ b/pcap-bpf.c @@ -455,7 +455,6 @@ pcap_create_interface(const char *device _U_, char *ebuf) * We claim that we support microsecond and nanosecond time * stamps. */ - p->tstamp_precision_count = 2; p->tstamp_precision_list = malloc(2 * sizeof(u_int)); if (p->tstamp_precision_list == NULL) { pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, @@ -465,6 +464,7 @@ pcap_create_interface(const char *device _U_, char *ebuf) } p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; + p->tstamp_precision_count = 2; #endif /* BIOCSTSTAMP */ return (p); } diff --git a/pcap-dag.c b/pcap-dag.c index 4df15274..eefde6fd 100644 --- a/pcap-dag.c +++ b/pcap-dag.c @@ -1084,7 +1084,6 @@ pcap_t *dag_create(const char *device, char *ebuf, int *is_ours) * XXX Our native precision is 2^-32s, but libpcap doesn't support * power of two precisions yet. We can convert to either MICRO or NANO. */ - p->tstamp_precision_count = 2; p->tstamp_precision_list = malloc(2 * sizeof(u_int)); if (p->tstamp_precision_list == NULL) { pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, @@ -1094,6 +1093,7 @@ pcap_t *dag_create(const char *device, char *ebuf, int *is_ours) } p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; + p->tstamp_precision_count = 2; return p; } diff --git a/pcap-linux.c b/pcap-linux.c index a059b7b2..43276e60 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -353,7 +353,6 @@ pcap_create_interface(const char *device, char *ebuf) * microsecond or nanosecond time stamps on arbitrary * adapters? */ - handle->tstamp_precision_count = 2; handle->tstamp_precision_list = malloc(2 * sizeof(u_int)); if (handle->tstamp_precision_list == NULL) { pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, @@ -363,6 +362,7 @@ pcap_create_interface(const char *device, char *ebuf) } handle->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; handle->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; + handle->tstamp_precision_count = 2; struct pcap_linux *handlep = handle->priv; handlep->poll_breakloop_fd = eventfd(0, EFD_NONBLOCK); @@ -4651,15 +4651,21 @@ static const struct { /* * Set the list of time stamping types to include all types. */ -static void -iface_set_all_ts_types(pcap_t *handle) +static int +iface_set_all_ts_types(pcap_t *handle, char *ebuf) { u_int i; - handle->tstamp_type_count = NUM_SOF_TIMESTAMPING_TYPES; handle->tstamp_type_list = malloc(NUM_SOF_TIMESTAMPING_TYPES * sizeof(u_int)); + if (handle->tstamp_type_list == NULL) { + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); + return -1; + } for (i = 0; i < NUM_SOF_TIMESTAMPING_TYPES; i++) handle->tstamp_type_list[i] = sof_ts_type_map[i].pcap_tstamp_val; + handle->tstamp_type_count = NUM_SOF_TIMESTAMPING_TYPES; + return 0; } #ifdef ETHTOOL_GET_TS_INFO @@ -4715,7 +4721,8 @@ iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf) * asking for the time stamping types, so let's * just return all the possible types. */ - iface_set_all_ts_types(handle); + if (iface_set_all_ts_types(handle, ebuf) == -1) + return -1; return 0; case ENODEV: @@ -4763,15 +4770,20 @@ iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf) if (info.so_timestamping & sof_ts_type_map[i].soft_timestamping_val) num_ts_types++; } - handle->tstamp_type_count = num_ts_types; if (num_ts_types != 0) { handle->tstamp_type_list = malloc(num_ts_types * sizeof(u_int)); + if (handle->tstamp_type_list == NULL) { + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "malloc"); + return -1; + } for (i = 0, j = 0; i < NUM_SOF_TIMESTAMPING_TYPES; i++) { if (info.so_timestamping & sof_ts_type_map[i].soft_timestamping_val) { handle->tstamp_type_list[j] = sof_ts_type_map[i].pcap_tstamp_val; j++; } } + handle->tstamp_type_count = num_ts_types; } else handle->tstamp_type_list = NULL; @@ -4779,7 +4791,7 @@ iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf) } #else /* ETHTOOL_GET_TS_INFO */ static int -iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf _U_) +iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf) { /* * This doesn't apply to the "any" device; you can't say "turn on @@ -4797,7 +4809,8 @@ iface_ethtool_get_ts_info(const char *device, pcap_t *handle, char *ebuf _U_) * We don't have an ioctl to use to ask what's supported, * so say we support everything. */ - iface_set_all_ts_types(handle); + if (iface_set_all_ts_types(handle, ebuf) == -1) + return -1; return 0; } #endif /* ETHTOOL_GET_TS_INFO */ diff --git a/pcap-snf.c b/pcap-snf.c index 3c37abf3..a9162eba 100644 --- a/pcap-snf.c +++ b/pcap-snf.c @@ -548,7 +548,6 @@ snf_create(const char *device, char *ebuf, int *is_ours) /* * We support microsecond and nanosecond time stamps. */ - p->tstamp_precision_count = 2; p->tstamp_precision_list = malloc(2 * sizeof(u_int)); if (p->tstamp_precision_list == NULL) { pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, errno, @@ -558,6 +557,7 @@ snf_create(const char *device, char *ebuf, int *is_ours) } p->tstamp_precision_list[0] = PCAP_TSTAMP_PRECISION_MICRO; p->tstamp_precision_list[1] = PCAP_TSTAMP_PRECISION_NANO; + p->tstamp_precision_count = 2; p->activate_op = snf_activate; ps->snf_boardnum = boardnum; -- cgit v1.2.1