[mw-devel] [Git][arthur/mw][master] 2 commits: Allocate all needed memory in StackEvent at once
Andrew Price
welshbyte at sucs.org
Tue Nov 6 21:52:59 GMT 2018
Andrew Price pushed to branch master at Justin Mitchell / mw
Commits:
2207f886 by Andrew Price at 2018-11-06T20:31:08+00:00
Allocate all needed memory in StackEvent at once
Lots of constification needed to make sure pointers into the allocated
memory aren't freed.
- - - - -
c542a18f by Andrew Price at 2018-11-06T21:48:01+00:00
Replay: account for message body not being nul terminated
Turns out msg->bodylen doesn't include the '\0'. Luckily the json parser
doesn't go further than the first character after the outer '}'.
- - - - -
13 changed files:
- src/client/Parse.c
- src/client/Parse.h
- src/client/incoming.c
- src/client/incoming.h
- src/client/script.c
- src/client/script.h
- src/client/talker.c
- src/server/replay.c
- src/str_util.c
- src/str_util.h
- src/webclient/comms.c
- src/webclient/import.c
- src/webclient/import.h
Changes:
=====================================
src/client/Parse.c
=====================================
--- a/src/client/Parse.c
+++ b/src/client/Parse.c
@@ -87,9 +87,9 @@ int BoolOpt(const char *n)
return -1;
}
-char *remove_first_word(char *args)
+const char *skip_first_word(const char *args)
{
- char *ptr, *ptr2;
+ const char *ptr, *ptr2;
ptr = args;
while (ptr && *ptr && isspace(*ptr)) ptr++;
@@ -97,7 +97,7 @@ char *remove_first_word(char *args)
{
ptr2 = strchr(ptr, ' ');
if (ptr2 != NULL) ptr2++;
- if (ptr2 == NULL) return(NULL); else return(strdup(ptr2));
+ return ptr2;
} else return(NULL);
}
=====================================
src/client/Parse.h
=====================================
--- a/src/client/Parse.h
+++ b/src/client/Parse.h
@@ -91,6 +91,6 @@ extern void c_alias(CommandList *cm, int argc, const char **argv, char *args);
extern void c_unalias(CommandList *cm, int argc, const char **argv, char *args);
void help_list(CommandList *c, unsigned int hidestuff, const char * const prefix);
-char *remove_first_word(char *args);
+const char *skip_first_word(const char *args);
#endif
=====================================
src/client/incoming.c
=====================================
--- a/src/client/incoming.c
+++ b/src/client/incoming.c
@@ -82,12 +82,19 @@ static void InsertMesg(struct mstack *new, ipc_message_t *msg)
static void StackEvent(char *text, char *from, int flags)
{
+ size_t textlen = strlen(text) + 1;
+ size_t fromlen = strlen(from) + 1;
struct mstack *new;
- new=(struct mstack *)malloc(sizeof(struct mstack));
- new->text=(char *)malloc(strlen(text)+1);
- new->from=(char *)malloc(strlen(from)+1);
- strcpy(new->text,text);
- strcpy(new->from,from);
+ char *ntext;
+ char *nfrom;
+
+ new = malloc(sizeof(*new) + textlen + fromlen);
+ ntext = (char *)(new + 1);
+ nfrom = ((char *)(new + 1)) + textlen;
+ strcpy(ntext, text);
+ strcpy(nfrom, from);
+ new->text = ntext;
+ new->from = nfrom;
new->flags = MST_SCREV;
new->preamble = flags;
InsertMesg(new, NULL);
@@ -102,10 +109,8 @@ void ClearStack(void) {
MesgStack=old->next;
if (old->msg)
ipcmsg_destroy(old->msg);
- free(old->text);
- free(old->from);
free(old);
- };
+ }
MesgStacked=0;
}
@@ -144,11 +149,11 @@ void DisplayStack(void)
{
case EST_RPC:
{
- char *msg;
+ const char *msg;
char *funcname;
char callfunc[MAXTEXTLENGTH];
- msg = remove_first_word(new->text);
+ msg = skip_first_word(new->text);
snprintf(callfunc, (MAXTEXTLENGTH-1<strlen(new->text))?MAXTEXTLENGTH-1:strlen(new->text) - strlen(msg), "%s", new->text);
script_output=1;
@@ -157,7 +162,6 @@ void DisplayStack(void)
ExecEvent(funcname, msg, "RPC", new->from, 0);
free(funcname);
}
- free(msg);
break;
}
case EST_IPC:
@@ -291,8 +295,6 @@ void DisplayStack(void)
}
else display_message(new->text, new->flags & MST_BEEP, 1);
- free(new->text);
- free(new->from);
old=new->next;
free(new);
MesgStacked--;
=====================================
src/client/incoming.h
=====================================
--- a/src/client/incoming.h
+++ b/src/client/incoming.h
@@ -17,8 +17,8 @@
struct mstack
{
- char *text;
- char *from;
+ const char *text;
+ const char *from;
int flags;
int preamble; /* Extra chars added by global/timestamp/spy */
struct mstack *next;
=====================================
src/client/script.c
=====================================
--- a/src/client/script.c
+++ b/src/client/script.c
@@ -823,7 +823,7 @@ int ExecScript(const char *name, var_list_t *vars, int bound)
return(0);
}
-int ExecEvent(char *script, const char *text, const char *event, char *who, int pre)
+int ExecEvent(char *script, const char *text, const char *event, const char *who, int pre)
{
int retval;
var_list_t args;
@@ -874,7 +874,7 @@ int ExecEvent(char *script, const char *text, const char *event, char *who, int
}
-int ExecEvent2(char *script, const char *event, char *who, int pre, int numargs, char *aargs[])
+int ExecEvent2(char *script, const char *event, const char *who, int pre, int numargs, char *aargs[])
{
var_list_t args;
int retval, i;
=====================================
src/client/script.h
=====================================
--- a/src/client/script.h
+++ b/src/client/script.h
@@ -257,8 +257,8 @@ void var_key_dup(var_op_t *op);
extern void DoScript(char *line);
int ExecScript(const char *name, var_list_t *args, int bound);
-int ExecEvent(char *script, const char *text, const char *event, char *who, int pre);
-int ExecEvent2(char *script, const char *event, char *who, int pre, int numargs, char *aargs[]);
+int ExecEvent(char *script, const char *text, const char *event, const char *who, int pre);
+int ExecEvent2(char *script, const char *event, const char *who, int pre, int numargs, char *aargs[]);
void ListScript(const char *name);
void ListVars(const char *srch);
=====================================
src/client/talker.c
=====================================
--- a/src/client/talker.c
+++ b/src/client/talker.c
@@ -881,7 +881,7 @@ void t_ungag(CommandList *cm, int argc, const char **argv, char *args)
void t_zod(CommandList *cm, int argc, const char **argv, char *args)
{
- _autofree char *excuse = remove_first_word(args);
+ const char *excuse = skip_first_word(args);
ipc_message_t * msg = ipcmsg_create(IPC_ACTION, user->posn);
json_t * j = json_init(NULL);
json_addstring(j, "target", argv[1]);
@@ -895,7 +895,7 @@ void t_zod(CommandList *cm, int argc, const char **argv, char *args)
void t_mrod(CommandList *cm, int argc, const char **argv, char *args)
{
- _autofree char *excuse = remove_first_word(args);
+ const char *excuse = skip_first_word(args);
ipc_message_t * msg = ipcmsg_create(IPC_ACTION, user->posn);
json_t * j = json_init(NULL);
json_addstring(j, "target", argv[1]);
@@ -909,7 +909,7 @@ void t_mrod(CommandList *cm, int argc, const char **argv, char *args)
void t_kick(CommandList *cm, int argc, const char **argv, char *args)
{
- _autofree char *excuse = remove_first_word(args);
+ const char *excuse = skip_first_word(args);
ipc_message_t * msg = ipcmsg_create(IPC_ACTION, user->posn);
json_t * j = json_init(NULL);
json_addstring(j, "target", argv[1]);
@@ -924,7 +924,7 @@ void t_kick(CommandList *cm, int argc, const char **argv, char *args)
void t_remove(CommandList *cm, int argc, const char **argv, char *args)
{
- _autofree char *excuse = remove_first_word(args);
+ const char *excuse = skip_first_word(args);
ipc_message_t * msg = ipcmsg_create(IPC_ACTION, user->posn);
json_t * j = json_init(NULL);
json_addstring(j, "target", argv[1]);
=====================================
src/server/replay.c
=====================================
--- a/src/server/replay.c
+++ b/src/server/replay.c
@@ -311,7 +311,7 @@ int replay_init(void)
close(fd);
return 1;
}
- msg->body = malloc(msg->bodylen);
+ msg->body = malloc(msg->bodylen + 1);
if (msg->body == NULL) {
perror("Failed to allocate message read buffer");
free(msg);
@@ -325,6 +325,7 @@ int replay_init(void)
close(fd);
return 1;
}
+ msg->body[msg->bodylen] = '\0';
if (msg->head.serial > highest_serial) {
highest_serial = msg->head.serial;
store_next = store_wrap(i + 1);
=====================================
src/str_util.c
=====================================
--- a/src/str_util.c
+++ b/src/str_util.c
@@ -91,7 +91,7 @@ void string_add(char **str, const char *fmt, ...)
}
}
-int allspace(char *in)
+int allspace(const char *in)
{
if (!strcmp(in, "")) {
return(1);
=====================================
src/str_util.h
=====================================
--- a/src/str_util.h
+++ b/src/str_util.h
@@ -13,7 +13,7 @@ int stringcmp(const char *a, const char *b, int n);
void strip_str(char *string);
int get_rand(int min, int max);
void string_add(char **str, const char *fmt, ...) mw_printf_func(2, 3);
-int allspace(char *in);
+int allspace(const char *in);
void strlower(char *szString);
void escprintf(const char *szFormat, ...);
char *strip_colours(const char *text);
=====================================
src/webclient/comms.c
=====================================
--- a/src/webclient/comms.c
+++ b/src/webclient/comms.c
@@ -470,12 +470,12 @@ static int handle_command(CONNECTION *co)
return 1;
}else
if (co->authd && strncasecmp(buff, "replay ", 7)==0) {
- _autofree char *style=NULL;
- _autofree char *value=NULL;
_autofree char *line = NULL;
+ const char *style = NULL;
+ const char *value = NULL;
char *tmpu;
- style = remove_first_word(buff);
- value = remove_first_word(style);
+ style = skip_first_word(buff);
+ value = skip_first_word(style);
if ((tmpu = strchr(style, ' '))!=NULL) *tmpu=0;
asprintf(&line, "{\"%s\":%ld}", style, atol(value));
ipc_send_to_username(NULL, IPC_REPLAY, line);
=====================================
src/webclient/import.c
=====================================
--- a/src/webclient/import.c
+++ b/src/webclient/import.c
@@ -76,9 +76,9 @@ void show_chatprivs(unsigned long cp, char *tmp, int flag)
tmp[i]=0;
}
-char *remove_first_word(char *args)
+const char *skip_first_word(const char *args)
{
- char *ptr, *ptr2;
+ const char *ptr, *ptr2;
ptr = args;
while (ptr && *ptr && isspace(*ptr)) ptr++;
@@ -86,7 +86,7 @@ char *remove_first_word(char *args)
{
ptr2 = strchr(ptr, ' ');
if (ptr2 != NULL) ptr2++;
- if (ptr2 == NULL) return(NULL); else return(strdup(ptr2));
+ return ptr2;
} else return(NULL);
}
=====================================
src/webclient/import.h
=====================================
--- a/src/webclient/import.h
+++ b/src/webclient/import.h
@@ -8,7 +8,7 @@ char *quotetext(const char *a);
void broadcast_onoffcode(int code, int method, const char *sourceuser, const char *reason);
void show_chatmodes(unsigned long cm, char *tmp, int flag);
void show_chatprivs(unsigned long cp, char *tmp, int flag);
-char *remove_first_word(char *args) ;
+const char *skip_first_word(const char *args);
void talk_send_to_room(const char *text, int channel, const char *type, int plural);
#endif /* IMPORT_H */
View it on GitLab: https://projects.sucs.org/arthur/mw/compare/9b0b4545bc8c10c5c9a85d1d238d665c106e931a...c542a18fff38d6ee49871a042f1e166a3e1c5ab1
--
View it on GitLab: https://projects.sucs.org/arthur/mw/compare/9b0b4545bc8c10c5c9a85d1d238d665c106e931a...c542a18fff38d6ee49871a042f1e166a3e1c5ab1
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20181106/045108a1/attachment-0001.html>
More information about the mw-devel
mailing list