Skip to content

Commit d1cac16

Browse files
Seija KijinLumioseSil
authored andcommitted
Prefer size_t over int where possible
This avoids needless truncation, which takes up multiple more instructions on some architectures, especially older ones. Signed-off-by: Seija Kijin <doremylover123@gmail.com>
1 parent acd6f47 commit d1cac16

12 files changed

Lines changed: 68 additions & 63 deletions

File tree

chat/chat.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,14 @@ struct termios saved_tty_parameters;
178178

179179
char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
180180
fail_buffer[50];
181-
int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
181+
size_t n_aborts = 0;
182+
int abort_next = 0, timeout_next = 0, echo_next = 0;
182183
int clear_abort_next = 0;
183184

184185
char *report_string[MAX_REPORTS] ;
185186
char report_buffer[4096] ;
186-
int n_reports = 0, report_next = 0, report_gathering = 0 ;
187+
size_t n_reports = 0;
188+
int report_next = 0, report_gathering = 0;
187189
int clear_report_next = 0;
188190

189191
int say_next = 0, hup_next = 0;
@@ -606,15 +608,15 @@ void terminate(int status)
606608
* Allow the last of the report string to be gathered before we terminate.
607609
*/
608610
if (report_gathering) {
609-
int c, rep_len;
611+
int c;
610612

611-
rep_len = strlen(report_buffer);
613+
size_t rep_len = strlen(report_buffer);
612614
while (rep_len + 1 < sizeof(report_buffer)) {
613615
alarm(1);
614616
c = get_char();
615617
alarm(0);
616618
if (c < 0 || iscntrl(c))
617-
break;
619+
break;
618620
report_buffer[rep_len] = c;
619621
++rep_len;
620622
}
@@ -1342,7 +1344,8 @@ int echo_stderr(int n)
13421344
int get_string(register char *string)
13431345
{
13441346
char temp[STR_LEN];
1345-
int c, printed = 0, len, minlen;
1347+
int c, printed = 0;
1348+
size_t len, minlen;
13461349
register char *s = temp, *end = s + STR_LEN;
13471350
char *s1, *logged = temp;
13481351

@@ -1372,11 +1375,11 @@ int get_string(register char *string)
13721375
alarmed = 0;
13731376

13741377
while ( ! alarmed && (c = get_char()) >= 0) {
1375-
int n, abort_len, report_len;
1378+
size_t n, abort_len, report_len;
13761379

13771380
if (echo) {
13781381
if (echo_stderr(c) != 0) {
1379-
fatal(2, "Could not write to stderr, %m");
1382+
fatal(2, "Could not write to stderr, %m");
13801383
}
13811384
}
13821385
if (verbose && c == '\n') {
@@ -1421,8 +1424,8 @@ int get_string(register char *string)
14211424
}
14221425
else {
14231426
if (!iscntrl (c)) {
1424-
int rep_len = strlen (report_buffer);
1425-
if ((rep_len + 1) < sizeof(report_buffer)) {
1427+
size_t rep_len = strlen (report_buffer);
1428+
if (rep_len < sizeof(report_buffer) - 1) {
14261429
report_buffer[rep_len] = c;
14271430
report_buffer[rep_len + 1] = '\0';
14281431
}

pppd/auth.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ static int
501501
setupapfile(char **argv)
502502
{
503503
FILE *ufile;
504-
int l;
504+
size_t l;
505505
uid_t euid;
506506
char u[MAXNAMELEN], p[MAXSECRETLEN];
507507
char *fname;
@@ -593,7 +593,7 @@ static int
593593
set_noauth_addr(char **argv)
594594
{
595595
char *addr = *argv;
596-
int l = strlen(addr) + 1;
596+
size_t l = strlen(addr) + 1;
597597
struct wordlist *wp;
598598

599599
wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
@@ -614,7 +614,7 @@ static int
614614
set_permitted_number(char **argv)
615615
{
616616
char *number = *argv;
617-
int l = strlen(number) + 1;
617+
size_t l = strlen(number) + 1;
618618
struct wordlist *wp;
619619

620620
wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
@@ -1831,7 +1831,8 @@ get_secret(int unit, char *client, char *server,
18311831
char *secret, int *secret_len, int am_server)
18321832
{
18331833
FILE *f;
1834-
int ret, len;
1834+
int ret;
1835+
size_t len;
18351836
char *filename;
18361837
struct wordlist *addrs, *opts;
18371838
char secbuf[MAXWORDLEN];
@@ -2154,22 +2155,24 @@ int
21542155
auth_number(void)
21552156
{
21562157
struct wordlist *wp = permitted_numbers;
2157-
int l;
2158+
size_t l;
21582159

21592160
/* Allow all if no authorization list. */
21602161
if (!wp)
21612162
return 1;
21622163

21632164
/* Allow if we have a match in the authorization list. */
2164-
while (wp) {
2165+
do {
21652166
/* trailing '*' wildcard */
21662167
l = strlen(wp->word);
2168+
if (l == 0)
2169+
return 1;
21672170
if ((wp->word)[l - 1] == '*')
21682171
l--;
21692172
if (!strncasecmp(wp->word, remote_number, l))
21702173
return 1;
21712174
wp = wp->next;
2172-
}
2175+
} while (wp);
21732176

21742177
return 0;
21752178
}

pppd/chap.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ chap_client_timeout(void *arg)
310310
static void
311311
chap_generate_challenge(struct chap_server_state *ss)
312312
{
313-
int clen = 1, nlen, len;
313+
size_t clen = 1, nlen, len;
314314
unsigned char *p;
315315

316316
p = ss->challenge;
@@ -327,8 +327,8 @@ chap_generate_challenge(struct chap_server_state *ss)
327327
p = ss->challenge + PPP_HDRLEN;
328328
p[0] = CHAP_CHALLENGE;
329329
p[1] = ++ss->id;
330-
p[2] = len >> 8;
331-
p[3] = len;
330+
p[2] = (len >> 8) & 0xFF;
331+
p[3] = len & 0xFF;
332332
}
333333

334334
/*

pppd/main.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,13 +1863,13 @@ update_script_environment(void)
18631863
struct userenv *uep;
18641864

18651865
for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1866-
int i;
1867-
char *p, *newstring;
1868-
int nlen = strlen(uep->ue_name);
1866+
size_t i;
1867+
char *p, *newstring;
1868+
size_t nlen = strlen(uep->ue_name);
18691869

1870-
for (i = 0; (p = script_env[i]) != NULL; i++) {
1871-
if (strncmp(p, uep->ue_name, nlen) == 0 && p[nlen] == '=')
1872-
break;
1870+
for (i = 0; (p = script_env[i]) != NULL; i++) {
1871+
if (strncmp(p, uep->ue_name, nlen) == 0 && p[nlen] == '=')
1872+
break;
18731873
}
18741874
if (uep->ue_isset) {
18751875
nlen += strlen(uep->ue_value) + 2;
@@ -2164,7 +2164,7 @@ ppp_script_setenv(char *var, char *value, int iskey)
21642164
{
21652165
size_t varl = strlen(var);
21662166
size_t vl = varl + strlen(value) + 2;
2167-
int i;
2167+
size_t i;
21682168
char *p, *newstring;
21692169

21702170
newstring = (char *) malloc(vl+1);
@@ -2223,14 +2223,14 @@ ppp_script_setenv(char *var, char *value, int iskey)
22232223
void
22242224
ppp_script_unsetenv(char *var)
22252225
{
2226-
int vl = strlen(var);
2226+
size_t vl = strlen(var);
22272227
int i;
22282228
char *p;
22292229

22302230
if (script_env == 0)
2231-
return;
2231+
return;
22322232
for (i = 0; (p = script_env[i]) != 0; ++i) {
2233-
if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
2233+
if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
22342234
#ifdef PPP_WITH_TDB
22352235
if (p[-1] && pppdb != NULL)
22362236
delete_db_key(p);

pppd/multilink.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ static void remove_bundle_link(void)
348348
TDB_DATA key, rec;
349349
char entry[32];
350350
char *p, *q;
351-
int l;
351+
size_t l;
352352

353353
key.dptr = blinks_id;
354354
key.dsize = strlen(blinks_id);
@@ -540,7 +540,7 @@ str_to_epdisc(struct epdisc *ep, char *str)
540540
char *p, *endp;
541541

542542
for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
543-
int sl = strlen(endp_class_names[i]);
543+
size_t sl = strlen(endp_class_names[i]);
544544
if (strncasecmp(str, endp_class_names[i], sl) == 0) {
545545
str += sl;
546546
break;

pppd/options.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,8 @@ static int
16141614
callfile(char **argv)
16151615
{
16161616
char *fname, *arg, *p;
1617-
int l, ok;
1617+
size_t l;
1618+
int ok;
16181619

16191620
arg = *argv;
16201621
ok = 1;
@@ -1769,7 +1770,7 @@ loadplugin(char **argv)
17691770

17701771
if (strchr(arg, '/') == 0) {
17711772
const char *base = PPP_PATH_PLUGIN;
1772-
int l = strlen(base) + strlen(arg) + 2;
1773+
size_t l = strlen(base) + strlen(arg) + 2;
17731774
path = malloc(l);
17741775
if (path == 0)
17751776
novm("plugin file path");
@@ -1832,9 +1833,8 @@ user_setenv(char **argv)
18321833
return 0;
18331834
}
18341835
for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
1835-
int nlen = strlen(uep->ue_name);
1836-
if (nlen == (eqp - arg) &&
1837-
strncmp(arg, uep->ue_name, nlen) == 0)
1836+
size_t nlen = strlen(uep->ue_name);
1837+
if (nlen == (eqp - arg) && strncmp(arg, uep->ue_name, nlen) == 0)
18381838
break;
18391839
}
18401840
/* Ignore attempts by unprivileged users to override privileged sources */

pppd/plugins/pppoatm/atm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,6 @@ int sap2text(char *buffer,int length,const struct atm_sap *sap,int flags);
103103
int sap_equal(const struct atm_sap *a,const struct atm_sap *b,int flags,...);
104104

105105
int __t2q_get_rate(const char **text,int up);
106-
int __atmlib_fetch(const char **pos,...); /* internal use only */
106+
size_t __atmlib_fetch(const char **pos,...); /* internal use only */
107107

108108
#endif

pppd/plugins/pppoatm/misc.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@
1717
#include <atmsap.h>
1818

1919

20-
int __atmlib_fetch(const char **pos,...)
20+
size_t __atmlib_fetch(const char **pos,...)
2121
{
2222
const char *value;
23-
int ref_len,best_len,len;
24-
int i,best;
23+
size_t ref_len,best_len,len;
24+
size_t i,best;
2525
va_list ap;
2626

2727
va_start(ap,pos);
2828
ref_len = strlen(*pos);
2929
best_len = 0;
30-
best = -1;
30+
best = 0;
3131
for (i = 0; (value = va_arg(ap,const char *)); i++) {
32-
len = strlen(value);
33-
if (*value != '!' && len <= ref_len && len > best_len &&
34-
!strncasecmp(*pos,value,len)) {
35-
best = i;
36-
best_len = len;
37-
}
32+
len = strlen(value);
33+
if (*value != '!' && len <= ref_len && len > best_len &&
34+
!strncasecmp(*pos,value,len)) {
35+
best = i;
36+
best_len = len;
37+
}
3838
}
3939
va_end(ap);
40-
if (best > -1) (*pos) += best_len;
40+
(*pos) += best_len;
4141
return best;
4242
}
4343

pppd/plugins/pppoatm/text2qos.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ int text2qos(const char *text,struct atm_qos *qos,int flags)
129129
traffic_class = ATM_NONE;
130130
aal = ATM_NO_AAL;
131131
do {
132-
static const unsigned char aal_number[] = { ATM_AAL0, ATM_AAL5 };
133-
int item;
132+
static const unsigned char aal_number[] = { ATM_AAL0, ATM_AAL5 };
133+
size_t item;
134134

135-
item = fetch(&text,"!none","ubr","cbr","vbr","abr","aal0","aal5",NULL);
135+
item = fetch(&text,"!none","ubr","cbr","vbr","abr","aal0","aal5",NULL);
136136
switch (item) {
137137
case 1:
138138
case 2:

pppd/plugins/pppoe/pppoe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ void pppoe_log_packet(const char *prefix, PPPoEPacket *packet);
284284

285285
static inline int parseHostUniq(const char *uniq, PPPoETag *tag)
286286
{
287-
unsigned i, len = strlen(uniq);
287+
size_t i, len = strlen(uniq);
288288

289289
#define hex(x) \
290290
(((x) <= '9') ? ((x) - '0') : \

0 commit comments

Comments
 (0)