Skip to content

Commit

Permalink
Prefer size_t over int where possible
Browse files Browse the repository at this point in the history
This avoids needless truncation, which takes up multiple more instructions on some architectures, especially older ones.

Signed-off-by: Seija Kijin <doremylover123@gmail.com>
  • Loading branch information
DTeachs authored and AreaZR committed Dec 21, 2023
1 parent acd6f47 commit d1cac16
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 63 deletions.
23 changes: 13 additions & 10 deletions chat/chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ struct termios saved_tty_parameters;

char *abort_string[MAX_ABORTS], *fail_reason = (char *)0,
fail_buffer[50];
int n_aborts = 0, abort_next = 0, timeout_next = 0, echo_next = 0;
size_t n_aborts = 0;
int abort_next = 0, timeout_next = 0, echo_next = 0;
int clear_abort_next = 0;

char *report_string[MAX_REPORTS] ;
char report_buffer[4096] ;
int n_reports = 0, report_next = 0, report_gathering = 0 ;
size_t n_reports = 0;
int report_next = 0, report_gathering = 0;
int clear_report_next = 0;

int say_next = 0, hup_next = 0;
Expand Down Expand Up @@ -606,15 +608,15 @@ void terminate(int status)
* Allow the last of the report string to be gathered before we terminate.
*/
if (report_gathering) {
int c, rep_len;
int c;

rep_len = strlen(report_buffer);
size_t rep_len = strlen(report_buffer);
while (rep_len + 1 < sizeof(report_buffer)) {
alarm(1);
c = get_char();
alarm(0);
if (c < 0 || iscntrl(c))
break;
break;
report_buffer[rep_len] = c;
++rep_len;
}
Expand Down Expand Up @@ -1342,7 +1344,8 @@ int echo_stderr(int n)
int get_string(register char *string)
{
char temp[STR_LEN];
int c, printed = 0, len, minlen;
int c, printed = 0;
size_t len, minlen;
register char *s = temp, *end = s + STR_LEN;
char *s1, *logged = temp;

Expand Down Expand Up @@ -1372,11 +1375,11 @@ int get_string(register char *string)
alarmed = 0;

while ( ! alarmed && (c = get_char()) >= 0) {
int n, abort_len, report_len;
size_t n, abort_len, report_len;

if (echo) {
if (echo_stderr(c) != 0) {
fatal(2, "Could not write to stderr, %m");
fatal(2, "Could not write to stderr, %m");
}
}
if (verbose && c == '\n') {
Expand Down Expand Up @@ -1421,8 +1424,8 @@ int get_string(register char *string)
}
else {
if (!iscntrl (c)) {
int rep_len = strlen (report_buffer);
if ((rep_len + 1) < sizeof(report_buffer)) {
size_t rep_len = strlen (report_buffer);
if (rep_len < sizeof(report_buffer) - 1) {
report_buffer[rep_len] = c;
report_buffer[rep_len + 1] = '\0';
}
Expand Down
17 changes: 10 additions & 7 deletions pppd/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ static int
setupapfile(char **argv)
{
FILE *ufile;
int l;
size_t l;
uid_t euid;
char u[MAXNAMELEN], p[MAXSECRETLEN];
char *fname;
Expand Down Expand Up @@ -593,7 +593,7 @@ static int
set_noauth_addr(char **argv)
{
char *addr = *argv;
int l = strlen(addr) + 1;
size_t l = strlen(addr) + 1;
struct wordlist *wp;

wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
Expand All @@ -614,7 +614,7 @@ static int
set_permitted_number(char **argv)
{
char *number = *argv;
int l = strlen(number) + 1;
size_t l = strlen(number) + 1;
struct wordlist *wp;

wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
Expand Down Expand Up @@ -1831,7 +1831,8 @@ get_secret(int unit, char *client, char *server,
char *secret, int *secret_len, int am_server)
{
FILE *f;
int ret, len;
int ret;
size_t len;
char *filename;
struct wordlist *addrs, *opts;
char secbuf[MAXWORDLEN];
Expand Down Expand Up @@ -2154,22 +2155,24 @@ int
auth_number(void)
{
struct wordlist *wp = permitted_numbers;
int l;
size_t l;

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

/* Allow if we have a match in the authorization list. */
while (wp) {
do {
/* trailing '*' wildcard */
l = strlen(wp->word);
if (l == 0)
return 1;
if ((wp->word)[l - 1] == '*')
l--;
if (!strncasecmp(wp->word, remote_number, l))
return 1;
wp = wp->next;
}
} while (wp);

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions pppd/chap.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ chap_client_timeout(void *arg)
static void
chap_generate_challenge(struct chap_server_state *ss)
{
int clen = 1, nlen, len;
size_t clen = 1, nlen, len;
unsigned char *p;

p = ss->challenge;
Expand All @@ -327,8 +327,8 @@ chap_generate_challenge(struct chap_server_state *ss)
p = ss->challenge + PPP_HDRLEN;
p[0] = CHAP_CHALLENGE;
p[1] = ++ss->id;
p[2] = len >> 8;
p[3] = len;
p[2] = (len >> 8) & 0xFF;
p[3] = len & 0xFF;
}

/*
Expand Down
20 changes: 10 additions & 10 deletions pppd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1863,13 +1863,13 @@ update_script_environment(void)
struct userenv *uep;

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

for (i = 0; (p = script_env[i]) != NULL; i++) {
if (strncmp(p, uep->ue_name, nlen) == 0 && p[nlen] == '=')
break;
for (i = 0; (p = script_env[i]) != NULL; i++) {
if (strncmp(p, uep->ue_name, nlen) == 0 && p[nlen] == '=')
break;
}
if (uep->ue_isset) {
nlen += strlen(uep->ue_value) + 2;
Expand Down Expand Up @@ -2164,7 +2164,7 @@ ppp_script_setenv(char *var, char *value, int iskey)
{
size_t varl = strlen(var);
size_t vl = varl + strlen(value) + 2;
int i;
size_t i;
char *p, *newstring;

newstring = (char *) malloc(vl+1);
Expand Down Expand Up @@ -2223,14 +2223,14 @@ ppp_script_setenv(char *var, char *value, int iskey)
void
ppp_script_unsetenv(char *var)
{
int vl = strlen(var);
size_t vl = strlen(var);
int i;
char *p;

if (script_env == 0)
return;
return;
for (i = 0; (p = script_env[i]) != 0; ++i) {
if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
if (strncmp(p, var, vl) == 0 && p[vl] == '=') {
#ifdef PPP_WITH_TDB
if (p[-1] && pppdb != NULL)
delete_db_key(p);
Expand Down
4 changes: 2 additions & 2 deletions pppd/multilink.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ static void remove_bundle_link(void)
TDB_DATA key, rec;
char entry[32];
char *p, *q;
int l;
size_t l;

key.dptr = blinks_id;
key.dsize = strlen(blinks_id);
Expand Down Expand Up @@ -540,7 +540,7 @@ str_to_epdisc(struct epdisc *ep, char *str)
char *p, *endp;

for (i = EPD_NULL; i <= EPD_PHONENUM; ++i) {
int sl = strlen(endp_class_names[i]);
size_t sl = strlen(endp_class_names[i]);
if (strncasecmp(str, endp_class_names[i], sl) == 0) {
str += sl;
break;
Expand Down
10 changes: 5 additions & 5 deletions pppd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,8 @@ static int
callfile(char **argv)
{
char *fname, *arg, *p;
int l, ok;
size_t l;
int ok;

arg = *argv;
ok = 1;
Expand Down Expand Up @@ -1769,7 +1770,7 @@ loadplugin(char **argv)

if (strchr(arg, '/') == 0) {
const char *base = PPP_PATH_PLUGIN;
int l = strlen(base) + strlen(arg) + 2;
size_t l = strlen(base) + strlen(arg) + 2;
path = malloc(l);
if (path == 0)
novm("plugin file path");
Expand Down Expand Up @@ -1832,9 +1833,8 @@ user_setenv(char **argv)
return 0;
}
for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
int nlen = strlen(uep->ue_name);
if (nlen == (eqp - arg) &&
strncmp(arg, uep->ue_name, nlen) == 0)
size_t nlen = strlen(uep->ue_name);
if (nlen == (eqp - arg) && strncmp(arg, uep->ue_name, nlen) == 0)
break;
}
/* Ignore attempts by unprivileged users to override privileged sources */
Expand Down
2 changes: 1 addition & 1 deletion pppd/plugins/pppoatm/atm.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ int sap2text(char *buffer,int length,const struct atm_sap *sap,int flags);
int sap_equal(const struct atm_sap *a,const struct atm_sap *b,int flags,...);

int __t2q_get_rate(const char **text,int up);
int __atmlib_fetch(const char **pos,...); /* internal use only */
size_t __atmlib_fetch(const char **pos,...); /* internal use only */

#endif
22 changes: 11 additions & 11 deletions pppd/plugins/pppoatm/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@
#include <atmsap.h>


int __atmlib_fetch(const char **pos,...)
size_t __atmlib_fetch(const char **pos,...)
{
const char *value;
int ref_len,best_len,len;
int i,best;
size_t ref_len,best_len,len;
size_t i,best;
va_list ap;

va_start(ap,pos);
ref_len = strlen(*pos);
best_len = 0;
best = -1;
best = 0;
for (i = 0; (value = va_arg(ap,const char *)); i++) {
len = strlen(value);
if (*value != '!' && len <= ref_len && len > best_len &&
!strncasecmp(*pos,value,len)) {
best = i;
best_len = len;
}
len = strlen(value);
if (*value != '!' && len <= ref_len && len > best_len &&
!strncasecmp(*pos,value,len)) {
best = i;
best_len = len;
}
}
va_end(ap);
if (best > -1) (*pos) += best_len;
(*pos) += best_len;
return best;
}

Expand Down
6 changes: 3 additions & 3 deletions pppd/plugins/pppoatm/text2qos.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ int text2qos(const char *text,struct atm_qos *qos,int flags)
traffic_class = ATM_NONE;
aal = ATM_NO_AAL;
do {
static const unsigned char aal_number[] = { ATM_AAL0, ATM_AAL5 };
int item;
static const unsigned char aal_number[] = { ATM_AAL0, ATM_AAL5 };
size_t item;

item = fetch(&text,"!none","ubr","cbr","vbr","abr","aal0","aal5",NULL);
item = fetch(&text,"!none","ubr","cbr","vbr","abr","aal0","aal5",NULL);
switch (item) {
case 1:
case 2:
Expand Down
2 changes: 1 addition & 1 deletion pppd/plugins/pppoe/pppoe.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void pppoe_log_packet(const char *prefix, PPPoEPacket *packet);

static inline int parseHostUniq(const char *uniq, PPPoETag *tag)
{
unsigned i, len = strlen(uniq);
size_t i, len = strlen(uniq);

#define hex(x) \
(((x) <= '9') ? ((x) - '0') : \
Expand Down
11 changes: 6 additions & 5 deletions pppd/tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,13 @@ static int tls_verify_callback(int ok, X509_STORE_CTX *ctx)

/* Match the suffix of common name */
if (!strcmp(TLS_VERIFY_SUFFIX, tls_verify_method)) {
int len = strlen(ptr1);
int off = strlen(cn_str) - len;
size_t len1, len2;
ptr2 = cn_str;
if (off > 0) {
ptr2 = cn_str + off;
}

len1 = strlen(ptr1);
len2 = strlen(ptr2);
if (len2 > len1)
ptr2 += len2 - len1;
}

if (strcmp(ptr1, ptr2)) {
Expand Down
8 changes: 3 additions & 5 deletions pppd/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,10 @@ log_write(int level, char *buf)
{
syslog(level, "%s", buf);
if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
int n = strlen(buf);

if (n > 0 && buf[n-1] == '\n')
size_t n = strlen(buf);
if (n > 0 && buf[n - 1] == '\n')
--n;
if (write(log_to_fd, buf, n) != n
|| write(log_to_fd, "\n", 1) != 1)
if (write(log_to_fd, buf, n) != n || write(log_to_fd, "\n", 1) != 1)
log_to_fd = -1;
}
}
Expand Down

0 comments on commit d1cac16

Please sign in to comment.