aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--command.c21
-rw-r--r--util.c9
-rw-r--r--util.h1
3 files changed, 19 insertions, 12 deletions
diff --git a/command.c b/command.c
index 51acfb6..5121ddf 100644
--- a/command.c
+++ b/command.c
@@ -255,9 +255,8 @@ static struct cmd_retval cmd_send(struct conn_data *data,const char *tag,const c
static struct cmd_retval history_cmd_helper(
struct conn_data *data,const char *tag,const char **args,
const char *cmdname,i64 beforeid){
- char *endp;
- i64 nrequested=strtoll(args[1],&endp,10);
- if(args[1][0]=='\0'||*endp!='\0'||nrequested<0){
+ i64 nrequested;
+ if(!parse_i64(args[1],&nrequested)||nrequested<0){
debug("Connection fd=%d sent an invalid number for '%s': '%s'",
data->fd,cmdname,args[1]);
return RET_CLOSE(true);
@@ -308,9 +307,8 @@ static struct cmd_retval cmd_history(struct conn_data *data,const char *tag,cons
}
static struct cmd_retval cmd_history_before(struct conn_data *data,const char *tag,const char **args){
- char *endp;
- i64 beforeid=strtoll(args[2],&endp,10);
- if(args[2][0]=='\0'||*endp!='\0'){
+ i64 beforeid;
+ if(!parse_i64(args[2],&beforeid)){
debug("Connection fd=%d sent an invalid id for 'history_before': '%s'",
data->fd,args[2]);
return RET_CLOSE(true);
@@ -359,9 +357,8 @@ static struct cmd_retval cmd_user_active(struct conn_data *data,const char *tag,
net_send_error(data->fd,tag,"Not logged in");
return RET_OK;
}
- char *endp;
- i64 active=strtoll(args[0],&endp,10);
- if(args[0][0]=='\0'||*endp!='\0'||active<0){
+ i64 active;
+ if(!parse_i64(args[0],&active)||active<0){
debug("Connection fd=%d sent an invalid number for 'user_active': '%s'",data->fd,args[0]);
return RET_CLOSE(true);
}
@@ -404,15 +401,15 @@ bool handle_input_line(struct conn_data *data,char *line,size_t linelen){
debug("No space in input line from connection %d",data->fd);
return true;
}
- char *tag=line;
- size_t taglen=sepp-tag;
+ const char *tag=line;
+ const size_t taglen=sepp-tag;
*sepp='\0';
line+=taglen+1;
linelen-=taglen+1;
sepp=memchr(line,' ',linelen);
if(sepp==NULL)sepp=line+linelen;
- size_t cmdlen=sepp-line;
+ const size_t cmdlen=sepp-line;
size_t cmdi;
for(cmdi=0;cmdi<NCOMMANDS;cmdi++){
if(cmdlen==strlen(commands[cmdi].cmdname)&&
diff --git a/util.c b/util.c
index 6887ce9..cc7ccc0 100644
--- a/util.c
+++ b/util.c
@@ -7,3 +7,12 @@ i64 make_timestamp(void){
gettimeofday(&tv,NULL);
return (i64)tv.tv_sec*1000000+tv.tv_usec;
}
+
+bool parse_i64(const char *str, i64 *out) {
+ if (str[0] == '\0') return false;
+ char *endp;
+ i64 result = strtoll(str, &endp, 10);
+ if (*endp != '\0') return false;
+ *out = result;
+ return true;
+}
diff --git a/util.h b/util.h
index e161efe..b6845da 100644
--- a/util.h
+++ b/util.h
@@ -4,3 +4,4 @@
i64 make_timestamp(void);
+bool parse_i64(const char *str, i64 *out);