[mw-devel] MW3 r1088 - trunk/src

arthur at sucs.org arthur at sucs.org
Wed Jul 29 17:20:10 BST 2009


Author: arthur
Date: 2009-07-29 17:20:10 +0100 (Wed, 29 Jul 2009)
New Revision: 1088

Modified:
   trunk/src/Makefile
   trunk/src/js.c
   trunk/src/log.c
   trunk/src/log.h
Log:
remove the thread unsafe code from the webget string builder code, use a dynamic block struct instead. fixes #47


Modified: trunk/src/Makefile
===================================================================
--- trunk/src/Makefile	2009-06-22 10:39:10 UTC (rev 1087)
+++ trunk/src/Makefile	2009-07-29 16:20:10 UTC (rev 1088)
@@ -44,7 +44,7 @@
 
 ### uncomment for gdb debugging
 LDFLAGS+= -ggdb -g
-CFLAGS+= -ggdb -g -D__NO_STRING_INLINE
+CFLAGS+= -ggdb -g -D__NO_STRING_INLINE -fstack-protector-all
 
 ### Optimisation - uncomment for release & extra testing
 CFLAGS+=-O3

Modified: trunk/src/js.c
===================================================================
--- trunk/src/js.c	2009-06-22 10:39:10 UTC (rev 1087)
+++ trunk/src/js.c	2009-07-29 16:20:10 UTC (rev 1088)
@@ -23,6 +23,7 @@
 #include "iconv.h"
 #include "alias.h"
 #include "alarm.h"
+#include "log.h"
 
 extern Alias alias_list;
 extern Alias bind_list;
@@ -749,23 +750,10 @@
  size_t  urldata(  void  *ptr,  size_t  size, size_t nmemb, void *stream)
 {
 	int addsize = size*nmemb;
-	char **data = stream;
-	static int datasize = 0;
-	int offset;
+	struct block_t *b = stream;
 
 	if (stream == NULL) return 0;
-	/* a new one, zero the count, otherwise extend and append */
-	if (*data == NULL) {
-		offset = 0;
-		datasize = addsize + 1;
-	} else  {
-		offset = datasize - 1;
-		datasize += addsize;
-	}
-	/* resize, copy on the end, and null terminate */
-	*data = realloc(*data, datasize);
-	memcpy( *data + offset, ptr, addsize );
-	memset( *data + datasize - 1, 0, 1);
+	block_append(b, ptr, addsize);
 	return addsize;
 }
 
@@ -789,14 +777,14 @@
 			if( conversion_result >= 0) {
 				CURL *cl;
 				char cerr[CURL_ERROR_SIZE];
-				char *answer = NULL;
+				struct block_t *answer = block_new(1024);
 
 				if( conversion_result & WOUTPUTTOOSHORT ) {
 					printf("JavaScript geturl() command: URL too long. It was truncated\n");
 				}
 				cl = curl_easy_init();
 				curl_easy_setopt(cl, CURLOPT_WRITEFUNCTION, urldata);
-				curl_easy_setopt(cl, CURLOPT_WRITEDATA, &answer);
+				curl_easy_setopt(cl, CURLOPT_WRITEDATA, answer);
 				curl_easy_setopt(cl, CURLOPT_URL, msg);
 				curl_easy_setopt(cl, CURLOPT_ERRORBUFFER, cerr);
 #ifdef RELEASE
@@ -811,7 +799,7 @@
                         fprintf(stderr, "JavaScript urlget failed %s: %s\n", msg, cerr);
                 curl_easy_cleanup(cl);
 
-		jsstr = JS_NewStringCopyZ(cx, answer);
+		jsstr = JS_NewStringCopyZ(cx, answer->p_buffer);
 		*rval = STRING_TO_JSVAL(jsstr);
 
 				free(answer);

Modified: trunk/src/log.c
===================================================================
--- trunk/src/log.c	2009-06-22 10:39:10 UTC (rev 1087)
+++ trunk/src/log.c	2009-07-29 16:20:10 UTC (rev 1088)
@@ -25,6 +25,7 @@
 #include "script.h"
 #include "js.h"
 #include "files.h"
+#include "log.h"
 
 #include "rooms.h"
 #include "ipc.h"
@@ -264,10 +265,10 @@
 	char *url;
 	CURL *c;
 	char *md = NULL;
-	char *deli=NULL;
-	char *body=NULL;
 	char *title=NULL;
 	char flags[1024];
+	struct block_t * deli=block_new(1024);
+	struct block_t * body=block_new(4096);
 
 	c = curl_easy_init();
 
@@ -282,33 +283,33 @@
 	curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1);
 	curl_easy_setopt(c, CURLOPT_MAXREDIRS, 5);
 	curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, urldata);
-	curl_easy_setopt(c, CURLOPT_WRITEDATA, &deli);
+	curl_easy_setopt(c, CURLOPT_WRITEDATA, deli);
 	curl_easy_perform(c);
 
-	if (deli != NULL && *deli == 0) {
-		free(deli);
+	if (deli->i_used < 2) {
+		block_free(deli);
 		deli=NULL;
 	}
 
 	/* this means, they havent heard of it */
-	if (deli != NULL && strncmp(deli,"[]",2)==0) {
-		free(deli);
+	if (deli != NULL && deli->i_used > 2 && strncmp(deli->p_buffer,"[]",2)==0) {
+		block_free(deli);
 		deli=NULL;
 	}
 
 	/* grab the pages title */
 	curl_easy_setopt(c, CURLOPT_URL, url);
-	curl_easy_setopt(c, CURLOPT_WRITEDATA, &body);
+	curl_easy_setopt(c, CURLOPT_WRITEDATA, body);
 	curl_easy_perform(c);
 
-	if (body != NULL) {
-		title = extract_title(body);
-		free(body);
+	if (body->i_used > 0) {
+		title = extract_title(body->p_buffer);
 		if (title != NULL && *title == 0) {
 			free(title);
 			title=NULL;
 		}
 	}
+	block_free(body);
 
 	/* fishing mission complete, store the results */
 
@@ -327,7 +328,7 @@
 	}
 
 	snprintf(path,sizeof(path),"%s/mwuri.db", STATEDIR);
-	char *query = sqlite3_mprintf("INSERT INTO mwuri (user, url, added, flags, title, tags) values (%Q,%Q,datetime('now'),%Q,%Q,%Q)", user->name, url, (uri->flags&URLFLAG_NSFW)?"nsfw":NULL, title, deli);
+	char *query = sqlite3_mprintf("INSERT INTO mwuri (user, url, added, flags, title, tags) values (%Q,%Q,datetime('now'),%Q,%Q,%Q)", user->name, url, (uri->flags&URLFLAG_NSFW)?"nsfw":NULL, title, deli==NULL?NULL:deli->p_buffer);
 	res = db_query(path, query, 1);
 	if (res == NULL) {
 		res = db_query(path, "CREATE TABLE mwuri (id INTEGER PRIMARY KEY AUTOINCREMENT, user text, url text, added text, flags text, title text, tags text )", 0);
@@ -340,7 +341,7 @@
 	sqlite3_free(query);
 	free(url);
 	if (title) free(title);
-	if (title) free(deli);
+	if (deli) block_free(deli);
 	curl_easy_cleanup(c);
 	free(uri->url);
 	free(uri); /* aka data */
@@ -390,3 +391,62 @@
 	db_free(res);
 	sqlite3_free(query);
 }
+
+
+/* block handling functions */
+struct block_t * block_new(int size)
+{
+	struct block_t *new = malloc(sizeof(struct block_t));
+	new->p_buffer = malloc(size);
+	bzero(new->p_buffer, size);
+	new->i_size = size;
+	new->i_used = 0;
+	new->p_cursor = new->p_buffer;
+	return new;
+}
+
+void block_free(struct block_t *p)
+{
+	if (p==NULL) return;
+	if (p->p_buffer) free(p->p_buffer);
+	p->p_buffer = NULL;
+	p->i_size = 0;
+	free(p);
+}
+
+void block_resize(struct block_t *old, int size)
+{
+	int offset;
+
+	if (old == NULL) return;
+	offset = old->p_cursor - old->p_buffer;
+	if (size > old->i_size) {
+		old->p_buffer = realloc(old->p_buffer, size);
+		old->i_size = size;
+		old->p_cursor = old->p_buffer + offset;
+	}
+}
+
+int block_append(struct block_t *p, void *data, int size)
+{
+	if (p == NULL) return 1;
+	if (p->i_used + size + 1 > p->i_size)
+		block_resize(p, p->i_used + size + 1);
+	memcpy(&p->p_buffer[p->i_used], data, size);
+	p->i_used += size;
+	p->p_buffer[p->i_used] = 0;
+	return 0;
+}
+
+int block_strappend(struct block_t *p, char *data)
+{
+	int size = strlen(data);
+
+	if (p == NULL) return 1;
+	if (p->i_used + size + 1 > p->i_size)
+		block_resize(p, p->i_used + size + 1);
+	memcpy(&p->p_buffer[p->i_used], data, size);
+	p->i_used += size;
+	p->p_buffer[p->i_used] = 0;
+	return 0;
+}

Modified: trunk/src/log.h
===================================================================
--- trunk/src/log.h	2009-06-22 10:39:10 UTC (rev 1087)
+++ trunk/src/log.h	2009-07-29 16:20:10 UTC (rev 1088)
@@ -1,3 +1,17 @@
 /* log.c */
 void catchuri(const char *what);
 void catchdoing(const char *what);
+
+struct block_t {
+	char *p_buffer; 	/* the actual data block */
+	int i_size;		/* actual block size */
+
+	int     i_used;		/* current fill level */
+	char *p_cursor;		/* user pointer */
+};      
+
+struct block_t * block_new(int);
+void block_free(struct block_t *);
+void block_resize(struct block_t *, int);
+int block_append(struct block_t *p, void *data, int size);
+int block_strappend(struct block_t *p, char *data);




More information about the mw-devel mailing list