[CLEANUP] add a few "const char *" where appropriate

As suggested by Markus Elfring, a few "const char *" have replaced
some "char *" declarations where a function is not expected to
modify a value. It does not change the code but it helps detecting
coding errors.
This commit is contained in:
Willy Tarreau 2006-10-15 15:17:57 +02:00
parent c642348ce4
commit b17916e89b
13 changed files with 54 additions and 50 deletions

View File

@ -32,9 +32,9 @@
extern int cfg_maxpconn; extern int cfg_maxpconn;
extern int cfg_maxconn; extern int cfg_maxconn;
int cfg_parse_global(char *file, int linenum, char **args); int cfg_parse_global(const char *file, int linenum, char **args);
int cfg_parse_listen(char *file, int linenum, char **args); int cfg_parse_listen(const char *file, int linenum, char **args);
int readcfgfile(char *file); int readcfgfile(const char *file);
#endif /* _COMMON_CFGPARSE_H */ #endif /* _COMMON_CFGPARSE_H */

View File

@ -41,16 +41,17 @@
struct hdr_exp { struct hdr_exp {
struct hdr_exp *next; struct hdr_exp *next;
regex_t *preg; /* expression to look for */ const regex_t *preg; /* expression to look for */
int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */ int action; /* ACT_ALLOW, ACT_REPLACE, ACT_REMOVE, ACT_DENY */
char *replace; /* expression to set instead */ const char *replace; /* expression to set instead */
}; };
extern regmatch_t pmatch[MAX_MATCH]; extern regmatch_t pmatch[MAX_MATCH];
int exp_replace(char *dst, char *src, char *str, regmatch_t *matches); int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches);
char *check_replace_string(char *str); const char *check_replace_string(const char *str);
char *chain_regex(struct hdr_exp **head, regex_t *preg, int action, char *replace); const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
int action, const char *replace);
#endif /* _COMMON_REGEX_H */ #endif /* _COMMON_REGEX_H */

View File

@ -39,31 +39,31 @@ extern struct timeval start_date; /* the process's start date */
/* /*
* adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
*/ */
struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms); struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms);
/* /*
* compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
* Must not be used when either argument is eternity. Use tv_cmp2_ms() for that. * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that.
*/ */
int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2); int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2);
/* /*
* compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
* considering that 0 is the eternity. * considering that 0 is the eternity.
*/ */
int tv_cmp2(struct timeval *tv1, struct timeval *tv2); int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2);
/* /*
* compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
* considering that 0 is the eternity. * considering that 0 is the eternity.
*/ */
int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2); int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2);
/* /*
* returns the remaining time between tv1=now and event=tv2 * returns the remaining time between tv1=now and event=tv2
* if tv2 is passed, 0 is returned. * if tv2 is passed, 0 is returned.
* Returns TIME_ETERNITY if tv2 is eternity. * Returns TIME_ETERNITY if tv2 is eternity.
*/ */
unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2); unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2);
/* sets <tv> to the current time */ /* sets <tv> to the current time */
@ -78,7 +78,7 @@ static inline struct timeval *tv_now(struct timeval *tv)
* compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
* Must not be used when either argument is eternity. Use tv_cmp2() for that. * Must not be used when either argument is eternity. Use tv_cmp2() for that.
*/ */
static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) static inline int tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
{ {
if (tv1->tv_sec < tv2->tv_sec) if (tv1->tv_sec < tv2->tv_sec)
return -1; return -1;
@ -96,7 +96,7 @@ static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2)
* returns the difference, in ms, between tv1 and tv2 * returns the difference, in ms, between tv1 and tv2
* Must not be used when either argument is eternity. * Must not be used when either argument is eternity.
*/ */
static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) static inline unsigned long tv_diff(const struct timeval *tv1, const struct timeval *tv2)
{ {
unsigned long ret; unsigned long ret;
@ -113,7 +113,7 @@ static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2)
* if tv2 is passed, 0 is returned. * if tv2 is passed, 0 is returned.
* Must not be used when either argument is eternity. * Must not be used when either argument is eternity.
*/ */
static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) static inline unsigned long tv_remain(const struct timeval *tv1, const struct timeval *tv2)
{ {
unsigned long ret; unsigned long ret;
@ -142,7 +142,7 @@ static inline struct timeval *tv_eternity(struct timeval *tv)
/* /*
* returns 1 if tv is null, else 0 * returns 1 if tv is null, else 0
*/ */
static inline int tv_iseternity(struct timeval *tv) static inline int tv_iseternity(const struct timeval *tv)
{ {
if (tv->tv_sec == 0 && tv->tv_usec == 0) if (tv->tv_sec == 0 && tv->tv_usec == 0)
return 1; return 1;
@ -154,8 +154,9 @@ static inline int tv_iseternity(struct timeval *tv)
* returns the first event between tv1 and tv2 into tvmin. * returns the first event between tv1 and tv2 into tvmin.
* a zero tv is ignored. tvmin is returned. * a zero tv is ignored. tvmin is returned.
*/ */
static inline struct timeval *tv_min(struct timeval *tvmin, static inline const struct timeval *tv_min(struct timeval *tvmin,
struct timeval *tv1, struct timeval *tv2) const struct timeval *tv1,
const struct timeval *tv2)
{ {
if (tv_cmp2(tv1, tv2) <= 0) if (tv_cmp2(tv1, tv2) <= 0)

View File

@ -97,7 +97,8 @@ static inline struct server *get_server_rr(struct proxy *px)
* If any server is found, it will be returned. If no valid server is found, * If any server is found, it will be returned. If no valid server is found,
* NULL is returned. * NULL is returned.
*/ */
static inline struct server *get_server_sh(struct proxy *px, char *addr, int len) static inline struct server *get_server_sh(const struct proxy *px,
const char *addr, int len)
{ {
unsigned int h, l; unsigned int h, l;

View File

@ -36,13 +36,13 @@ static inline void buffer_init(struct buffer *buf)
} }
/* returns 1 if the buffer is empty, 0 otherwise */ /* returns 1 if the buffer is empty, 0 otherwise */
static inline int buffer_isempty(struct buffer *buf) static inline int buffer_isempty(const struct buffer *buf)
{ {
return buf->l == 0; return buf->l == 0;
} }
/* returns 1 if the buffer is full, 0 otherwise */ /* returns 1 if the buffer is full, 0 otherwise */
static inline int buffer_isfull(struct buffer *buf) { static inline int buffer_isfull(const struct buffer *buf) {
return buf->l == BUFSIZE; return buf->l == BUFSIZE;
} }
@ -55,7 +55,7 @@ static inline void buffer_flush(struct buffer *buf)
/* returns the maximum number of bytes writable at once in this buffer */ /* returns the maximum number of bytes writable at once in this buffer */
static inline int buffer_max(struct buffer *buf) static inline int buffer_max(const struct buffer *buf)
{ {
if (buf->l == BUFSIZE) if (buf->l == BUFSIZE)
return 0; return 0;

View File

@ -36,7 +36,7 @@ int process_srv(struct session *t);
void client_retnclose(struct session *s, int len, const char *msg); void client_retnclose(struct session *s, int len, const char *msg);
void client_return(struct session *s, int len, const char *msg); void client_return(struct session *s, int len, const char *msg);
void srv_close_with_err(struct session *t, int err, int finst, void srv_close_with_err(struct session *t, int err, int finst,
int status, int msglen, char *msg); int status, int msglen, const char *msg);
int produce_content(struct session *s); int produce_content(struct session *s);

View File

@ -36,14 +36,14 @@ struct session *pendconn_get_next_sess(struct server *srv, struct proxy *px);
struct pendconn *pendconn_add(struct session *sess); struct pendconn *pendconn_add(struct session *sess);
void pendconn_free(struct pendconn *p); void pendconn_free(struct pendconn *p);
int process_srv_queue(struct task *t); int process_srv_queue(struct task *t);
unsigned int srv_dynamic_maxconn(struct server *s); unsigned int srv_dynamic_maxconn(const struct server *s);
/* Returns the first pending connection for server <s>, which may be NULL if /* Returns the first pending connection for server <s>, which may be NULL if
* nothing is pending. * nothing is pending.
*/ */
static inline struct pendconn *pendconn_from_srv(struct server *s) { static inline struct pendconn *pendconn_from_srv(const struct server *s) {
if (!s->nbpend) if (!s->nbpend)
return NULL; return NULL;
@ -53,7 +53,7 @@ static inline struct pendconn *pendconn_from_srv(struct server *s) {
/* Returns the first pending connection for proxy <px>, which may be NULL if /* Returns the first pending connection for proxy <px>, which may be NULL if
* nothing is pending. * nothing is pending.
*/ */
static inline struct pendconn *pendconn_from_px(struct proxy *px) { static inline struct pendconn *pendconn_from_px(const struct proxy *px) {
if (!px->nbpend) if (!px->nbpend)
return NULL; return NULL;
@ -63,7 +63,7 @@ static inline struct pendconn *pendconn_from_px(struct proxy *px) {
/* returns 0 if nothing has to be done for server <s> regarding queued connections, /* returns 0 if nothing has to be done for server <s> regarding queued connections,
* and non-zero otherwise. Suited for and if/else usage. * and non-zero otherwise. Suited for and if/else usage.
*/ */
static inline int may_dequeue_tasks(struct server *s, struct proxy *p) { static inline int may_dequeue_tasks(const struct server *s, const struct proxy *p) {
return (s && (s->nbpend || p->nbpend) && return (s && (s->nbpend || p->nbpend) &&
(!s->maxconn || s->cur_sess < srv_dynamic_maxconn(s)) && (!s->maxconn || s->cur_sess < srv_dynamic_maxconn(s)) &&
s->queue_mgt); s->queue_mgt);

View File

@ -251,7 +251,7 @@ static struct listener *str2listener(char *str, struct listener *tail)
/* /*
* parse a line in a <global> section. Returns 0 if OK, -1 if error. * parse a line in a <global> section. Returns 0 if OK, -1 if error.
*/ */
int cfg_parse_global(char *file, int linenum, char **args) int cfg_parse_global(const char *file, int linenum, char **args)
{ {
if (!strcmp(args[0], "global")) { /* new section */ if (!strcmp(args[0], "global")) { /* new section */
@ -424,11 +424,11 @@ static void init_default_instance()
/* /*
* parse a line in a <listen> section. Returns 0 if OK, -1 if error. * parse a line in a <listen> section. Returns 0 if OK, -1 if error.
*/ */
int cfg_parse_listen(char *file, int linenum, char **args) int cfg_parse_listen(const char *file, int linenum, char **args)
{ {
static struct proxy *curproxy = NULL; static struct proxy *curproxy = NULL;
struct server *newsrv = NULL; struct server *newsrv = NULL;
char *err; const char *err;
int rc; int rc;
if (!strcmp(args[0], "listen")) { /* new proxy */ if (!strcmp(args[0], "listen")) { /* new proxy */
@ -1776,7 +1776,7 @@ int cfg_parse_listen(char *file, int linenum, char **args)
* This function reads and parses the configuration file given in the argument. * This function reads and parses the configuration file given in the argument.
* returns 0 if OK, -1 if error. * returns 0 if OK, -1 if error.
*/ */
int readcfgfile(char *file) int readcfgfile(const char *file)
{ {
char thisline[256]; char thisline[256];
char *line; char *line;

View File

@ -64,7 +64,7 @@ void **pool_requri = NULL;
* Displays the message on stderr with the date and pid. Overrides the quiet * Displays the message on stderr with the date and pid. Overrides the quiet
* mode during startup. * mode during startup.
*/ */
void Alert(char *fmt, ...) void Alert(const char *fmt, ...)
{ {
va_list argp; va_list argp;
struct timeval tv; struct timeval tv;
@ -87,7 +87,7 @@ void Alert(char *fmt, ...)
/* /*
* Displays the message on stderr with the date and pid. * Displays the message on stderr with the date and pid.
*/ */
void Warning(char *fmt, ...) void Warning(const char *fmt, ...)
{ {
va_list argp; va_list argp;
struct timeval tv; struct timeval tv;
@ -109,7 +109,7 @@ void Warning(char *fmt, ...)
/* /*
* Displays the message on <out> only if quiet mode is not set. * Displays the message on <out> only if quiet mode is not set.
*/ */
void qfprintf(FILE *out, char *fmt, ...) void qfprintf(FILE *out, const char *fmt, ...)
{ {
va_list argp; va_list argp;
@ -173,7 +173,7 @@ fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set)
* It also tries not to waste too much time computing the message header. * It also tries not to waste too much time computing the message header.
* It doesn't care about errors nor does it report them. * It doesn't care about errors nor does it report them.
*/ */
void send_log(struct proxy *p, int level, char *message, ...) void send_log(struct proxy *p, int level, const char *message, ...)
{ {
static int logfd = -1; /* syslog UDP socket */ static int logfd = -1; /* syslog UDP socket */
static long tvsec = -1; /* to force the string to be initialized */ static long tvsec = -1; /* to force the string to be initialized */

View File

@ -117,7 +117,7 @@ void client_return(struct session *s, int len, const char *msg)
* returned. * returned.
*/ */
void srv_close_with_err(struct session *t, int err, int finst, void srv_close_with_err(struct session *t, int err, int finst,
int status, int msglen, char *msg) int status, int msglen, const char *msg)
{ {
t->srv_state = SV_STCLOSE; t->srv_state = SV_STCLOSE;
if (status > 0) { if (status > 0) {

View File

@ -26,7 +26,7 @@ void **pool_pendconn = NULL;
/* returns the effective dynamic maxconn for a server, considering the minconn /* returns the effective dynamic maxconn for a server, considering the minconn
* and the proxy's usage relative to its saturation. * and the proxy's usage relative to its saturation.
*/ */
unsigned int srv_dynamic_maxconn(struct server *s) unsigned int srv_dynamic_maxconn(const struct server *s)
{ {
return s->minconn ? return s->minconn ?
((s->maxconn * s->proxy->nbconn / s->proxy->maxconn) < s->minconn) ? s->minconn : ((s->maxconn * s->proxy->nbconn / s->proxy->maxconn) < s->minconn) ? s->minconn :

View File

@ -24,7 +24,7 @@ regmatch_t pmatch[MAX_MATCH]; /* rm_so, rm_eo for regular expressions */
int exp_replace(char *dst, char *src, char *str, regmatch_t *matches) int exp_replace(char *dst, char *src, const char *str, const regmatch_t *matches)
{ {
char *old_dst = dst; char *old_dst = dst;
@ -65,9 +65,9 @@ int exp_replace(char *dst, char *src, char *str, regmatch_t *matches)
} }
/* returns NULL if the replacement string <str> is valid, or the pointer to the first error */ /* returns NULL if the replacement string <str> is valid, or the pointer to the first error */
char *check_replace_string(char *str) const char *check_replace_string(const char *str)
{ {
char *err = NULL; const char *err = NULL;
while (*str) { while (*str) {
if (*str == '\\') { if (*str == '\\') {
err = str; /* in case of a backslash, we return the pointer to it */ err = str; /* in case of a backslash, we return the pointer to it */
@ -97,12 +97,13 @@ char *check_replace_string(char *str)
/* returns the pointer to an error in the replacement string, or NULL if OK */ /* returns the pointer to an error in the replacement string, or NULL if OK */
char *chain_regex(struct hdr_exp **head, regex_t *preg, int action, char *replace) const char *chain_regex(struct hdr_exp **head, const regex_t *preg,
int action, const char *replace)
{ {
struct hdr_exp *exp; struct hdr_exp *exp;
if (replace != NULL) { if (replace != NULL) {
char *err; const char *err;
err = check_replace_string(replace); err = check_replace_string(replace);
if (err) if (err)
return err; return err;

View File

@ -21,7 +21,7 @@ struct timeval start_date; /* the process's start date */
/* /*
* adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
*/ */
struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms) struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms)
{ {
if (!tv || !from) if (!tv || !from)
return NULL; return NULL;
@ -38,7 +38,7 @@ struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms)
* compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
* Must not be used when either argument is eternity. Use tv_cmp2_ms() for that. * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that.
*/ */
int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2) int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2)
{ {
if (tv1->tv_sec == tv2->tv_sec) { if (tv1->tv_sec == tv2->tv_sec) {
if (tv2->tv_usec >= tv1->tv_usec + 1000) if (tv2->tv_usec >= tv1->tv_usec + 1000)
@ -62,7 +62,7 @@ int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2)
* compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
* considering that 0 is the eternity. * considering that 0 is the eternity.
*/ */
int tv_cmp2(struct timeval *tv1, struct timeval *tv2) int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2)
{ {
if (tv_iseternity(tv1)) if (tv_iseternity(tv1))
if (tv_iseternity(tv2)) if (tv_iseternity(tv2))
@ -88,7 +88,7 @@ int tv_cmp2(struct timeval *tv1, struct timeval *tv2)
* compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
* considering that 0 is the eternity. * considering that 0 is the eternity.
*/ */
int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2) int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2)
{ {
if (tv_iseternity(tv1)) if (tv_iseternity(tv1))
if (tv_iseternity(tv2)) if (tv_iseternity(tv2))
@ -121,7 +121,7 @@ int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2)
* if tv2 is passed, 0 is returned. * if tv2 is passed, 0 is returned.
* Returns TIME_ETERNITY if tv2 is eternity. * Returns TIME_ETERNITY if tv2 is eternity.
*/ */
unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2) unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2)
{ {
unsigned long ret; unsigned long ret;
@ -144,7 +144,7 @@ unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2)
* returns the absolute difference, in ms, between tv1 and tv2 * returns the absolute difference, in ms, between tv1 and tv2
* Must not be used when either argument is eternity. * Must not be used when either argument is eternity.
*/ */
unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2) unsigned long tv_delta(const struct timeval *tv1, const struct timeval *tv2)
{ {
int cmp; int cmp;
unsigned long ret; unsigned long ret;
@ -154,7 +154,7 @@ unsigned long tv_delta(struct timeval *tv1, struct timeval *tv2)
if (!cmp) if (!cmp)
return 0; /* same dates, null diff */ return 0; /* same dates, null diff */
else if (cmp < 0) { else if (cmp < 0) {
struct timeval *tmp = tv1; const struct timeval *tmp = tv1;
tv1 = tv2; tv1 = tv2;
tv2 = tmp; tv2 = tmp;
} }