Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 8dc155e

Browse files
committed
Remove unnecessary casts
Unnecessary casts are a blight in that they mask possible programming errors. We shouldn't use them where they aren't required. Also, add a convenience function to convert a header pointer to a body pointer by adding the correct offset.
1 parent b2d3c0d commit 8dc155e

File tree

11 files changed

+50
-43
lines changed

11 files changed

+50
-43
lines changed

libtac/include/libtac.h

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ void tac_session_free(struct tac_session *);
167167
extern int tac_debug_enable;
168168
extern int tac_readtimeout_enable;
169169

170+
/* we return a void * because there are different types of bodies */
171+
static inline void *tac_hdr_to_body(HDR *th)
172+
{
173+
return (void *)((u_char *)th + TAC_PLUS_HDR_SIZE);
174+
}
175+
170176
HDR *_tac_req_header(struct tac_session *, u_char, bool);
171177

172178
/* connect.c */

libtac/lib/acct_r.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int tac_acct_parse(struct tac_session *sess, u_char *pkt, unsigned pkt_total,
5454

5555
ulen_from_header = ntohl(th->datalength);
5656

57-
tb = (struct acct_reply *)(pkt + TAC_PLUS_HDR_SIZE);
57+
tb = tac_hdr_to_body(th);
5858

5959
if (pkt_total != ulen_from_header) {
6060
TACSYSLOG(LOG_ERR,\
@@ -88,14 +88,14 @@ int tac_acct_parse(struct tac_session *sess, u_char *pkt, unsigned pkt_total,
8888

8989
/* save status and clean up */
9090
if(tb->msg_len) {
91-
msg=(char *) xcalloc(1, tb->msg_len+1);
91+
msg = xcalloc(1, tb->msg_len+1);
9292
bcopy((u_char *) tb+TAC_ACCT_REPLY_FIXED_FIELDS_SIZE, msg, tb->msg_len);
9393
msg[tb->msg_len] = '\0';
9494
re->msg = msg; /* Freed by caller */
9595
}
9696

9797
if(tb->data_len) {
98-
msg=(char *) xcalloc(1, tb->data_len+1);
98+
msg = xcalloc(1, tb->data_len+1);
9999
bcopy((u_char *) tb+TAC_ACCT_REPLY_FIXED_FIELDS_SIZE+tb->data_len,
100100
msg, tb->data_len);
101101
msg[tb->data_len] = '\0';
@@ -156,9 +156,9 @@ int tac_acct_read(struct tac_session *sess, struct areply *re) {
156156
return re->status;
157157
}
158158

159-
th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
159+
th = xcalloc(1, TAC_PLUS_HDR_SIZE);
160160

161-
spacket_read = read(sess->fd, (char *)th, TAC_PLUS_HDR_SIZE);
161+
spacket_read = read(sess->fd, th, TAC_PLUS_HDR_SIZE);
162162
if(spacket_read < TAC_PLUS_HDR_SIZE) {
163163
TACSYSLOG(LOG_ERR,\
164164
"%s: short reply header, read %zd of %u expected: %m", __FUNCTION__,\
@@ -181,8 +181,8 @@ int tac_acct_read(struct tac_session *sess, struct areply *re) {
181181
}
182182

183183
/* now make room for entire contiguous packet */
184-
th = (HDR *)xrealloc(th, TAC_PLUS_HDR_SIZE + ulen_from_header);
185-
tb = (struct acct_reply *)((u_char *)th + TAC_PLUS_HDR_SIZE);
184+
th = xrealloc(th, TAC_PLUS_HDR_SIZE + ulen_from_header);
185+
tb = tac_hdr_to_body(th);
186186

187187
/* read reply packet body */
188188
if (tac_readtimeout_enable &&
@@ -195,7 +195,7 @@ int tac_acct_read(struct tac_session *sess, struct areply *re) {
195195
return re->status;
196196
}
197197

198-
spacket_read = read(sess->fd, (char *)tb, ulen_from_header);
198+
spacket_read = read(sess->fd, tb, ulen_from_header);
199199
if(spacket_read < 0 || (size_t) spacket_read < ulen_from_header) {
200200
TACSYSLOG(LOG_ERR,\
201201
"%s: short reply body, read %zd of %zu: %m",\
@@ -208,7 +208,8 @@ int tac_acct_read(struct tac_session *sess, struct areply *re) {
208208
}
209209

210210
/* now parse remaining packet fields */
211-
status = tac_acct_parse(sess, (u_char *)th, TAC_PLUS_HDR_SIZE + ulen_from_header, re);
211+
status = tac_acct_parse(sess, (u_char *)th,
212+
TAC_PLUS_HDR_SIZE + ulen_from_header, re);
212213

213214
/* all useful data has been copied out */
214215
free(th);

libtac/lib/acct_s.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void tac_acct_send_pkt(struct tac_session *sess, u_char type,
7676
pkt_total += a->attr_len + 1; /* count length byte too */
7777
}
7878

79-
pkt = (u_char *)xcalloc(1, pkt_total);
79+
pkt = xcalloc(1, pkt_total);
8080
th = (HDR *)pkt;
8181

8282
/* tacacs header */
@@ -88,7 +88,7 @@ void tac_acct_send_pkt(struct tac_session *sess, u_char type,
8888
th->datalength = htonl(pkt_total - TAC_PLUS_HDR_SIZE);
8989

9090
/* fixed part of tacacs body */
91-
tb = (struct acct *)(pkt + TAC_PLUS_HDR_SIZE);
91+
tb = tac_hdr_to_body(th);
9292
tb->flags = type;
9393
tb->authen_method = sess->tac_authen_method;
9494
tb->priv_lvl = sess->tac_priv_lvl;

libtac/lib/attrib.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ void tac_add_attrib(struct tac_attrib **attr, char *name, char *value) {
2929

3030
void tac_add_attrib_pair(struct tac_attrib **attr, char *name, char sep, char *value) {
3131
struct tac_attrib *a;
32-
u_char l1 = (u_char) strlen(name);
33-
u_char l2;
34-
int total_len;
32+
unsigned l1 = (u_char) strlen(name);
33+
unsigned l2;
34+
unsigned total_len;
3535

3636
if (value == NULL) {
3737
l2 = 0;
3838
} else {
39-
l2 = (u_char) strlen(value);
39+
l2 = strlen(value);
4040
}
4141
total_len = l1 + l2 + 1; /* "name" + "=" + "value" */
4242

@@ -49,15 +49,15 @@ void tac_add_attrib_pair(struct tac_attrib **attr, char *name, char sep, char *v
4949

5050
/* initialize the list if application passed us a null pointer */
5151
if(*attr == NULL) {
52-
*attr = (struct tac_attrib *) xcalloc(1, sizeof(struct tac_attrib));
52+
*attr = xcalloc(1, sizeof(struct tac_attrib));
5353
a = *attr;
5454
} else {
5555
/* find the last allocated block */
5656
a = *attr;
5757
while(a->next != NULL)
5858
a = a->next; /* a holds last allocated block */
5959

60-
a->next = (struct tac_attrib *) xcalloc(1, sizeof(struct tac_attrib));
60+
a->next = xcalloc(1, sizeof(struct tac_attrib));
6161
a = a->next; /* set current block pointer to the new one */
6262
}
6363

@@ -67,7 +67,7 @@ void tac_add_attrib_pair(struct tac_attrib **attr, char *name, char sep, char *v
6767

6868
/* fill the block */
6969
a->attr_len=total_len;
70-
a->attr = (char *) xcalloc(1, total_len+1);
70+
a->attr = xcalloc(1, total_len+1);
7171
bcopy(name, a->attr, l1); /* paste name */
7272
*(a->attr+l1)=sep; /* insert seperator "[=*]" */
7373
if (value != NULL) {

libtac/lib/authen_r.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int tac_authen_parse(struct tac_session *sess, struct areply *re,
5252

5353
len_from_header = ntohl(th->datalength);
5454

55-
tb = (struct authen_reply *) (pkt + TAC_PLUS_HDR_SIZE);
55+
tb = tac_hdr_to_body(th);
5656

5757
if (pkt_total != TAC_PLUS_HDR_SIZE + len_from_header) {
5858
TACSYSLOG(
@@ -157,7 +157,7 @@ int tac_authen_read(struct tac_session *sess, struct areply *re) {
157157
return re->status;
158158
}
159159

160-
th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
160+
th = xcalloc(1, TAC_PLUS_HDR_SIZE);
161161

162162
r = read(sess->fd, th, TAC_PLUS_HDR_SIZE);
163163
if (r < TAC_PLUS_HDR_SIZE) {
@@ -181,8 +181,8 @@ int tac_authen_read(struct tac_session *sess, struct areply *re) {
181181
}
182182

183183
/* now make room for entire contiguous packet */
184-
th = (HDR *)xrealloc(th, TAC_PLUS_HDR_SIZE + len_from_header);
185-
tb = (struct authen_reply *) ((u_char *)th + TAC_PLUS_HDR_SIZE);
184+
th = xrealloc(th, TAC_PLUS_HDR_SIZE + len_from_header);
185+
tb = tac_hdr_to_body(th);
186186

187187
/* read reply packet body */
188188
if (tac_readtimeout_enable &&
@@ -192,7 +192,7 @@ int tac_authen_read(struct tac_session *sess, struct areply *re) {
192192
status = LIBTAC_STATUS_READ_TIMEOUT;
193193
}
194194

195-
r = read(sess->fd, (char *)tb, len_from_header);
195+
r = read(sess->fd, tb, len_from_header);
196196
if (r < 0 || (unsigned) r < len_from_header) {
197197
TACSYSLOG(LOG_ERR,
198198
"%s: short reply body, read %d of %zu: %m",

libtac/lib/authen_s.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void tac_authen_send_pkt(struct tac_session *sess,
141141
pkt_total = TAC_AUTHEN_START_FIXED_TOTAL +
142142
user_len + port_len + r_addr_len + token_len;
143143

144-
pkt = (u_char *)xcalloc(1, pkt_total);
144+
pkt = xcalloc(1, pkt_total);
145145
th = (HDR *)pkt;
146146

147147
/* set some header options */
@@ -159,7 +159,7 @@ void tac_authen_send_pkt(struct tac_session *sess,
159159
th->datalength = htonl(pkt_total - TAC_PLUS_HDR_SIZE);
160160

161161
/* fixed part of tacacs body */
162-
tb = (struct authen_start *)(pkt + TAC_PLUS_HDR_SIZE);
162+
tb = tac_hdr_to_body(th);
163163
tb->action = TAC_PLUS_AUTHEN_LOGIN;
164164
tb->priv_lvl = sess->tac_priv_lvl;
165165
if (sess->tac_authen_type == TAC_PLUS_AUTHEN_TYPE_PAP) {

libtac/lib/author_r.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int tac_author_parse(struct tac_session *sess,
5454

5555
len_from_header = ntohl(th->datalength);
5656

57-
tb = (struct author_reply *)(pkt + TAC_PLUS_HDR_SIZE);
57+
tb = tac_hdr_to_body(th);
5858

5959
if (pkt_total != TAC_PLUS_HDR_SIZE + len_from_header) {
6060
TACSYSLOG(
@@ -106,7 +106,7 @@ int tac_author_parse(struct tac_session *sess,
106106

107107
/* server message for user */
108108
if (tb->msg_len) {
109-
char *msg = (char *) xcalloc(1, tb->msg_len + 1);
109+
char *msg = xcalloc(1, tb->msg_len + 1);
110110
bcopy(
111111
(u_char *) tb + TAC_AUTHOR_REPLY_FIXED_FIELDS_SIZE
112112
+ (tb->arg_cnt) * sizeof(tb->arg_len[0]), msg, tb->msg_len);
@@ -116,7 +116,7 @@ int tac_author_parse(struct tac_session *sess,
116116

117117
/* server message to syslog */
118118
if (tb->data_len) {
119-
char *smsg = (char *) xcalloc(1, tb->data_len + 1);
119+
char *smsg = xcalloc(1, tb->data_len + 1);
120120
bcopy(
121121
(u_char *) tb + TAC_AUTHOR_REPLY_FIXED_FIELDS_SIZE
122122
+ (tb->arg_cnt) * sizeof(tb->arg_len[0]) + tb->msg_len, smsg,
@@ -248,7 +248,7 @@ int tac_author_read(struct tac_session *sess, struct areply *re) {
248248
return re->status;
249249
}
250250

251-
th = (HDR *)xcalloc(1, TAC_PLUS_HDR_SIZE);
251+
th = xcalloc(1, TAC_PLUS_HDR_SIZE);
252252

253253
packet_read = read(sess->fd, th, TAC_PLUS_HDR_SIZE);
254254
if (packet_read < TAC_PLUS_HDR_SIZE) {
@@ -272,8 +272,8 @@ int tac_author_read(struct tac_session *sess, struct areply *re) {
272272
}
273273

274274
/* now make room for entire contiguous packet */
275-
th = (HDR *)xrealloc(th, TAC_PLUS_HDR_SIZE + len_from_header);
276-
tb = (struct author_reply *)((u_char *)th + TAC_PLUS_HDR_SIZE);
275+
th = xrealloc(th, TAC_PLUS_HDR_SIZE + len_from_header);
276+
tb = tac_hdr_to_body(th);
277277

278278
/* read reply packet body */
279279
if (tac_readtimeout_enable

libtac/lib/author_s.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ void tac_author_send_pkt(struct tac_session *sess,
4242
/*
4343
* precompute the buffer size so we don't need to keep resizing/copying it
4444
*/
45-
user_len = (u_char) strlen(user);
46-
port_len = (u_char) strlen(tty);
47-
r_addr_len = (u_char) strlen(r_addr);
45+
user_len = strlen(user);
46+
port_len = strlen(tty);
47+
r_addr_len = strlen(r_addr);
4848

4949
assert(user_len <= UCHAR_MAX);
5050
assert(port_len <= UCHAR_MAX);
@@ -60,7 +60,7 @@ void tac_author_send_pkt(struct tac_session *sess,
6060
pkt_total += a->attr_len + 1; /* count length byte too */
6161
}
6262

63-
pkt = (u_char *)xcalloc(1, pkt_total);
63+
pkt = xcalloc(1, pkt_total);
6464
th = (HDR *)pkt;
6565

6666
/* tacacs header */
@@ -72,7 +72,7 @@ void tac_author_send_pkt(struct tac_session *sess,
7272
th->datalength = htonl(pkt_total - TAC_PLUS_HDR_SIZE);
7373

7474
/* fixed part of tacacs body */
75-
tb = (struct author *)(pkt + TAC_PLUS_HDR_SIZE);
75+
tb = tac_hdr_to_body(th);
7676
tb->authen_method = sess->tac_authen_method;
7777
tb->priv_lvl = sess->tac_priv_lvl;
7878
tb->authen_type = sess->tac_authen_type;

libtac/lib/cont_s.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void tac_cont_send_pkt(struct tac_session *sess, const char *pass,
4949
pkt_total = TAC_AUTHEN_CONT_FIXED_TOTAL + pass_len;
5050

5151
/* build the packet */
52-
pkt = (u_char *)xcalloc(1, pkt_total);
52+
pkt = xcalloc(1, pkt_total);
5353
th = (HDR *)pkt;
5454

5555
/* set some header options */
@@ -63,7 +63,7 @@ void tac_cont_send_pkt(struct tac_session *sess, const char *pass,
6363
th->datalength = htonl(pkt_total - TAC_PLUS_HDR_SIZE);
6464

6565
/* fixed part of tacacs body */
66-
tb = (struct authen_cont *)(pkt + TAC_PLUS_HDR_SIZE);
66+
tb = tac_hdr_to_body(th);
6767
tb->flags = 0;
6868
tb->user_msg_len = htons(pass_len);
6969
tb->user_data_len = 0;

libtac/lib/xalloc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void *xcalloc(size_t nmemb, size_t size) {
2626
void *val = calloc(nmemb, size);
2727
if (val == 0) {
2828
TACSYSLOG(
29-
LOG_ERR, "%s: calloc(%u,%u) failed", __FUNCTION__, (unsigned) nmemb, (unsigned) size);
29+
LOG_ERR, "%s: calloc(%zd,%zd) failed", __FUNCTION__, nmemb, size);
3030
exit(1);
3131
}
3232
return val;
@@ -36,7 +36,7 @@ void *xrealloc(void *ptr, size_t size) {
3636
void *val = realloc(ptr, size);
3737
if (val == 0) {
3838
TACSYSLOG(
39-
LOG_ERR, "%s: realloc(%u) failed", __FUNCTION__, (unsigned) size);
39+
LOG_ERR, "%s: realloc(%zd) failed", __FUNCTION__, size);
4040
exit(1);
4141
}
4242
return val;

support.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ char *_pam_get_user(pam_handle_t *pamh) {
5656
int retval;
5757
char *user;
5858

59-
retval = pam_get_user(pamh, (void *)&user, "Username: ");
59+
retval = pam_get_user(pamh, (const char **)&user, "Username: ");
6060
if (retval != PAM_SUCCESS || user == NULL || *user == '\0') {
6161
_pam_log(LOG_ERR, "unable to obtain username");
6262
user = NULL;
@@ -68,7 +68,7 @@ char *_pam_get_terminal(pam_handle_t *pamh) {
6868
int retval;
6969
char *tty;
7070

71-
retval = pam_get_item(pamh, PAM_TTY, (void *)&tty);
71+
retval = pam_get_item(pamh, PAM_TTY, (const void **)&tty);
7272
if (retval != PAM_SUCCESS || tty == NULL || *tty == '\0') {
7373
tty = ttyname(STDIN_FILENO);
7474
if(tty == NULL || *tty == '\0')
@@ -81,7 +81,7 @@ char *_pam_get_rhost(pam_handle_t *pamh) {
8181
int retval;
8282
char *rhost;
8383

84-
retval = pam_get_item(pamh, PAM_RHOST, (void *)&rhost);
84+
retval = pam_get_item(pamh, PAM_RHOST, (const void **)&rhost);
8585
if (retval != PAM_SUCCESS || rhost == NULL || *rhost == '\0') {
8686
rhost = "unknown";
8787
}

0 commit comments

Comments
 (0)