From 110849d42247943b54672d1b5301ee2d2d1d6164 Mon Sep 17 00:00:00 2001 From: Stephan Gatzka Date: Fri, 7 Feb 2014 17:52:03 +0100 Subject: [PATCH] Make functions and structs dealing with string literals const correct. This allows the compiler to check if someone writes into string literals. Signed-off-by: Stephan Gatzka --- msg.c | 2 +- msg.h | 2 +- pmc.c | 5 +++-- pmc_common.c | 2 +- pmc_common.h | 2 +- raw.c | 4 ++-- sk.c | 12 ++++++------ sk.h | 10 +++++----- transport.c | 2 +- transport.h | 2 +- transport_private.h | 2 +- udp.c | 4 ++-- udp6.c | 4 ++-- uds.c | 2 +- util.c | 6 +++--- util.h | 6 +++--- 16 files changed, 34 insertions(+), 33 deletions(-) diff --git a/msg.c b/msg.c index 4af9782..7edbdd2 100644 --- a/msg.c +++ b/msg.c @@ -379,7 +379,7 @@ int msg_pre_send(struct ptp_message *m) return 0; } -char *msg_type_string(int type) +const char *msg_type_string(int type) { switch (type) { case SYNC: diff --git a/msg.h b/msg.h index 1fad5e9..7f471ca 100644 --- a/msg.h +++ b/msg.h @@ -298,7 +298,7 @@ int msg_pre_send(struct ptp_message *m); * @param type Value of the messageType field as returned by @ref msg_type(). * @return String describing the message type. */ -char *msg_type_string(int type); +const char *msg_type_string(int type); /** * Print messages for debugging purposes. diff --git a/pmc.c b/pmc.c index 7ea0368..077991b 100644 --- a/pmc.c +++ b/pmc.c @@ -109,7 +109,7 @@ struct management_id idtab[] = { { "PORT_DATA_SET_NP", PORT_DATA_SET_NP, do_set_action }, }; -static char *action_string[] = { +static const char *action_string[] = { "GET", "SET", "RESPONSE", @@ -711,7 +711,8 @@ static void usage(char *progname) int main(int argc, char *argv[]) { - char *iface_name = NULL, *progname; + const char *iface_name = NULL; + char *progname; int c, cnt, length, tmo = -1, batch_mode = 0, zero_datalen = 0; char line[1024], *command = NULL; enum transport_type transport_type = TRANS_UDP_IPV4; diff --git a/pmc_common.c b/pmc_common.c index 9b0720f..e52d68f 100644 --- a/pmc_common.c +++ b/pmc_common.c @@ -61,7 +61,7 @@ struct pmc { int zero_length_gets; }; -struct pmc *pmc_create(enum transport_type transport_type, char *iface_name, +struct pmc *pmc_create(enum transport_type transport_type, const char *iface_name, UInteger8 boundary_hops, UInteger8 domain_number, UInteger8 transport_specific, int zero_datalen) { diff --git a/pmc_common.h b/pmc_common.h index a7b4ae7..850d85f 100644 --- a/pmc_common.h +++ b/pmc_common.h @@ -25,7 +25,7 @@ struct pmc; -struct pmc *pmc_create(enum transport_type transport_type, char *iface_name, +struct pmc *pmc_create(enum transport_type transport_type, const char *iface_name, UInteger8 boundary_hops, UInteger8 domain_number, UInteger8 transport_specific, int zero_datalen); diff --git a/raw.c b/raw.c index d87edbf..1bfc1bb 100644 --- a/raw.c +++ b/raw.c @@ -148,7 +148,7 @@ static int raw_close(struct transport *t, struct fdarray *fda) unsigned char ptp_dst_mac[MAC_LEN] = { PTP_DST_MAC }; unsigned char p2p_dst_mac[MAC_LEN] = { P2P_DST_MAC }; -static int open_socket(char *name, int event) +static int open_socket(const char *name, int event) { struct sockaddr_ll addr; int fd, index; @@ -184,7 +184,7 @@ no_socket: return -1; } -static int raw_open(struct transport *t, char *name, +static int raw_open(struct transport *t, const char *name, struct fdarray *fda, enum timestamp_type ts_type) { struct raw *raw = container_of(t, struct raw, t); diff --git a/sk.c b/sk.c index 1fbbcc5..8d70d1a 100644 --- a/sk.c +++ b/sk.c @@ -40,7 +40,7 @@ int sk_check_fupsync; /* private methods */ -static int hwts_init(int fd, char *device, int rx_filter, int one_step) +static int hwts_init(int fd, const char *device, int rx_filter, int one_step) { struct ifreq ifreq; struct hwtstamp_config cfg, req; @@ -77,7 +77,7 @@ static int hwts_init(int fd, char *device, int rx_filter, int one_step) /* public methods */ -int sk_interface_index(int fd, char *name) +int sk_interface_index(int fd, const char *name) { struct ifreq ifreq; int err; @@ -102,7 +102,7 @@ int sk_general_init(int fd) return 0; } -int sk_get_ts_info(char *name, struct sk_ts_info *sk_info) +int sk_get_ts_info(const char *name, struct sk_ts_info *sk_info) { #ifdef ETHTOOL_GET_TS_INFO struct ethtool_ts_info info; @@ -146,7 +146,7 @@ failed: return -1; } -int sk_interface_macaddr(char *name, unsigned char *mac, int len) +int sk_interface_macaddr(const char *name, unsigned char *mac, int len) { struct ifreq ifreq; int err, fd; @@ -172,7 +172,7 @@ int sk_interface_macaddr(char *name, unsigned char *mac, int len) return 0; } -int sk_interface_addr(char *name, int family, uint8_t *addr, int len) +int sk_interface_addr(const char *name, int family, uint8_t *addr, int len) { struct ifaddrs *ifaddr, *i; int copy_len, result = -1; @@ -285,7 +285,7 @@ int sk_receive(int fd, void *buf, int buflen, return cnt; } -int sk_timestamping_init(int fd, char *device, enum timestamp_type type, +int sk_timestamping_init(int fd, const char *device, enum timestamp_type type, enum transport_type transport) { int err, filter1, filter2 = 0, flags, one_step; diff --git a/sk.h b/sk.h index 895840f..ea338c1 100644 --- a/sk.h +++ b/sk.h @@ -44,7 +44,7 @@ struct sk_ts_info { * @param device The name of the network interface of interest. * @return The result from the SIOCGIFINDEX ioctl. */ -int sk_interface_index(int fd, char *device); +int sk_interface_index(int fd, const char *device); /** * Prepare a given socket for PTP "general" messages. @@ -59,7 +59,7 @@ int sk_general_init(int fd); * @param info Struct containing obtained timestamping information. * @return zero on success, negative on failure. */ -int sk_get_ts_info(char *name, struct sk_ts_info *sk_info); +int sk_get_ts_info(const char *name, struct sk_ts_info *sk_info); /** * Obtain the MAC address of a network interface. @@ -68,7 +68,7 @@ int sk_get_ts_info(char *name, struct sk_ts_info *sk_info); * @param len Length of 'mac' * @return Zero on success, non-zero otherwise. */ -int sk_interface_macaddr(char *name, unsigned char *mac, int len); +int sk_interface_macaddr(const char *name, unsigned char *mac, int len); /** * Obtains the first IP address assigned to a network interface. @@ -78,7 +78,7 @@ int sk_interface_macaddr(char *name, unsigned char *mac, int len); * @param len Length of 'addr' * @return The number of bytes written to addr on success, -1 otherwise. */ -int sk_interface_addr(char *name, int family, uint8_t *addr, int len); +int sk_interface_addr(const char *name, int family, uint8_t *addr, int len); /** * Read a message from a socket. @@ -100,7 +100,7 @@ int sk_receive(int fd, void *buf, int buflen, * @param transport The type of transport used. * @return Zero on success, non-zero otherwise. */ -int sk_timestamping_init(int fd, char *device, enum timestamp_type type, +int sk_timestamping_init(int fd, const char *device, enum timestamp_type type, enum transport_type transport); /** diff --git a/transport.c b/transport.c index 3c70b2b..b5346e5 100644 --- a/transport.c +++ b/transport.c @@ -29,7 +29,7 @@ int transport_close(struct transport *t, struct fdarray *fda) return t->close(t, fda); } -int transport_open(struct transport *t, char *name, +int transport_open(struct transport *t, const char *name, struct fdarray *fda, enum timestamp_type tt) { return t->open(t, name, fda, tt); diff --git a/transport.h b/transport.h index 34934fe..aa2018b 100644 --- a/transport.h +++ b/transport.h @@ -64,7 +64,7 @@ struct transport; int transport_close(struct transport *t, struct fdarray *fda); -int transport_open(struct transport *t, char *name, +int transport_open(struct transport *t, const char *name, struct fdarray *fda, enum timestamp_type tt); int transport_recv(struct transport *t, int fd, diff --git a/transport_private.h b/transport_private.h index 0c553e1..7124f94 100644 --- a/transport_private.h +++ b/transport_private.h @@ -30,7 +30,7 @@ struct transport { int (*close)(struct transport *t, struct fdarray *fda); - int (*open)(struct transport *t, char *name, struct fdarray *fda, + int (*open)(struct transport *t, const char *name, struct fdarray *fda, enum timestamp_type tt); int (*recv)(struct transport *t, int fd, void *buf, int buflen, diff --git a/udp.c b/udp.c index 9628291..818cbfd 100644 --- a/udp.c +++ b/udp.c @@ -93,7 +93,7 @@ static int udp_close(struct transport *t, struct fdarray *fda) return 0; } -static int open_socket(char *name, struct in_addr mc_addr[2], short port) +static int open_socket(const char *name, struct in_addr mc_addr[2], short port) { struct sockaddr_in addr; int fd, index, on = 1; @@ -148,7 +148,7 @@ enum { MC_PRIMARY, MC_PDELAY }; static struct in_addr mcast_addr[2]; -static int udp_open(struct transport *t, char *name, struct fdarray *fda, +static int udp_open(struct transport *t, const char *name, struct fdarray *fda, enum timestamp_type ts_type) { struct udp *udp = container_of(t, struct udp, t); diff --git a/udp6.c b/udp6.c index e0d1256..e26195b 100644 --- a/udp6.c +++ b/udp6.c @@ -100,7 +100,7 @@ static int udp6_close(struct transport *t, struct fdarray *fda) return 0; } -static int open_socket_ipv6(char *name, struct in6_addr mc_addr[2], short port, +static int open_socket_ipv6(const char *name, struct in6_addr mc_addr[2], short port, int *interface_index) { struct sockaddr_in6 addr; @@ -158,7 +158,7 @@ enum { MC_PRIMARY, MC_PDELAY }; static struct in6_addr mc6_addr[2]; -static int udp6_open(struct transport *t, char *name, struct fdarray *fda, +static int udp6_open(struct transport *t, const char *name, struct fdarray *fda, enum timestamp_type ts_type) { struct udp6 *udp6 = container_of(t, struct udp6, t); diff --git a/uds.c b/uds.c index 1dbfd6c..35f5ecc 100644 --- a/uds.c +++ b/uds.c @@ -46,7 +46,7 @@ static int uds_close(struct transport *t, struct fdarray *fda) return 0; } -static int uds_open(struct transport *t, char *name, struct fdarray *fda, +static int uds_open(struct transport *t, const char *name, struct fdarray *fda, enum timestamp_type tt) { int fd, err; diff --git a/util.c b/util.c index 4b0ef6b..56af8ef 100644 --- a/util.c +++ b/util.c @@ -28,7 +28,7 @@ #define NS_PER_HOUR (3600 * NS_PER_SEC) #define NS_PER_DAY (24 * NS_PER_HOUR) -char *ps_str[] = { +const char *ps_str[] = { "NONE", "INITIALIZING", "FAULTY", @@ -42,7 +42,7 @@ char *ps_str[] = { "GRAND_MASTER", }; -char *ev_str[] = { +const char *ev_str[] = { "NONE", "POWERUP", "INITIALIZE", @@ -98,7 +98,7 @@ int str2pid(const char *s, struct PortIdentity *result) return -1; } -int generate_clock_identity(struct ClockIdentity *ci, char *name) +int generate_clock_identity(struct ClockIdentity *ci, const char *name) { unsigned char mac[6]; if (sk_interface_macaddr(name, mac, sizeof(mac))) diff --git a/util.h b/util.h index 8cf3890..3fae51c 100644 --- a/util.h +++ b/util.h @@ -26,12 +26,12 @@ /** * Table of human readable strings, one for each port state. */ -extern char *ps_str[]; +extern const char *ps_str[]; /** * Table of human readable strings, one for each port event. */ -extern char *ev_str[]; +extern const char *ev_str[]; /** * Convert a clock identity into a human readable string. @@ -64,7 +64,7 @@ char *pid2str(struct PortIdentity *id); */ int str2pid(const char *s, struct PortIdentity *result); -int generate_clock_identity(struct ClockIdentity *ci, char *name); +int generate_clock_identity(struct ClockIdentity *ci, const char *name); /** * Copies a PTPText to a static_ptp_text. This copies the text into