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: Rose <[email protected]>
  • Loading branch information
AtariDreams committed May 22, 2024
1 parent 04e6b8d commit 78d3bac
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 45 deletions.
19 changes: 11 additions & 8 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;
unsigned long 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 ;
unsigned long 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,9 +608,9 @@ 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();
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,7 +1375,7 @@ 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) {
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
15 changes: 8 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,22 @@ 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 ((wp->word)[l - 1] == '*')
l--;
if (!strncasecmp(wp->word, remote_number, l))
return 1;
wp = wp->next;
}
} while (wp);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion 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 Down
10 changes: 5 additions & 5 deletions pppd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1863,9 +1863,9 @@ update_script_environment(void)
struct userenv *uep;

for (uep = userenv_list; uep != NULL; uep = uep->ue_next) {
int i;
size_t i;
char *p, *newstring;
int nlen = strlen(uep->ue_name);
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] == '=')
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,8 +2223,8 @@ ppp_script_setenv(char *var, char *value, int iskey)
void
ppp_script_unsetenv(char *var)
{
int vl = strlen(var);
int i;
size_t vl = strlen(var);
size_t i;
char *p;

if (script_env == 0)
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
10 changes: 5 additions & 5 deletions pppd/plugins/pppoatm/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
#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 &&
Expand All @@ -37,7 +37,7 @@ int __atmlib_fetch(const char **pos,...)
}
}
va_end(ap);
if (best > -1) (*pos) += best_len;
(*pos) += best_len;
return best;
}

Expand Down
2 changes: 1 addition & 1 deletion pppd/plugins/pppoatm/text2qos.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ int text2qos(const char *text,struct atm_qos *qos,int flags)
aal = ATM_NO_AAL;
do {
static const unsigned char aal_number[] = { ATM_AAL0, ATM_AAL5 };
int item;
size_t item;

item = fetch(&text,"!none","ubr","cbr","vbr","abr","aal0","aal5",NULL);
switch (item) {
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 @@ -285,7 +285,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
7 changes: 3 additions & 4 deletions pppd/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,9 @@ static void
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')
if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
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)
Expand Down

0 comments on commit 78d3bac

Please sign in to comment.