Mongoose Networking Library problem on Visual Studio

#1

Im using Cesantas Mongoose Network library and when I compile the library in Visual Studio 2015, the variables are uninitialized and it crashes, as the debug sets them to 0xCDCDCD o 0xFEFEFE and the library (mongoose.c) checks the variables to 0.

Any thoughts?

#2

I can’t reproduce this issue.
Created a console application and used the code from examples/simplest_web_server which compiles and runs fine. Configurations tested debug/release, x86/x64, Visual Studio Community 2015 and 2017.

Could you provide a simple example to reproduce?

#3

I just took the file from examples cookie_auth.c and mongoose.c, included the mongoose.h directory.
It crashes in console too. I use Visual Studio Enterprise 2015 14.0.

It crashes when I load the page on the navigator (chrome).
The callstack shows the crash on "get_session(http_message* hm) line 82 after a MG_EV_HTTP_REQUEST.

ucrtbased.dll!check_bytes(const unsigned char * const first, const unsigned char value, const unsigned int size) Line 194	C++
ucrtbased.dll!is_block_an_aligned_allocation(const void * const block) Line 251	C++
ucrtbased.dll!free_dbg_nolock(void * const block, const int block_use) Line 886	C++
ucrtbased.dll!_free_dbg(void * block, int block_use) Line 1030	C++
ucrtbased.dll!free(void * block) Line 28	C++
cooie_auth.exe!get_session(http_message * hm) Line 82	C

cooie_auth.exe!ev_handler(mg_connection * nc, int ev, void * p) Line 205 C
cooie_auth.exe!mg_call(mg_connection * nc, void(*)(mg_connection *, int, void ) ev_handler, void * user_data, int ev, void * ev_data) Line 2404 C
cooie_auth.exe!mg_http_call_endpoint_handler(mg_connection * nc, int ev, http_message * hm) Line 8822 C
cooie_auth.exe!mg_http_handler(mg_connection * nc, int ev, void * ev_data) Line 6629 C
cooie_auth.exe!mg_call(mg_connection * nc, void(
)(mg_connection *, int, void *) ev_handler, void * user_data, int ev, void * ev_data) Line 2404 C
cooie_auth.exe!mg_recv_tcp(mg_connection * nc, char * buf, unsigned int len) Line 2931 C
cooie_auth.exe!mg_do_recv(mg_connection * nc) Line 2887 C
cooie_auth.exe!mg_if_can_recv_cb(mg_connection * nc) Line 2893 C
cooie_auth.exe!mg_mgr_handle_conn(mg_connection * nc, int fd_flags, double now) Line 4009 C
cooie_auth.exe!mg_socket_if_poll(mg_iface * iface, int timeout_ms) Line 4200 C
cooie_auth.exe!mg_mgr_poll(mg_mgr * m, int timeout_ms) Line 2593 C
cooie_auth.exe!main() Line 261 C

#4

I tested the “simplest_webserver.c” and it works fine.
The example “cookie_auth.c” crashes in the same project.

#5

cookie_auth did not compile complaining about uninitialized variables in get_session.
Modified it like this and builds and runs ok.

static struct session *get_session(struct http_message *hm) {
	char ssid_buf[21];
	char *ssid = ssid_buf;
	struct session *ret = NULL;
	struct mg_str *cookie_header = mg_get_http_header(hm, "cookie");
	if (cookie_header == NULL) goto clean;
	if (!mg_http_parse_header2(cookie_header, SESSION_COOKIE_NAME, &ssid,
		sizeof(ssid_buf))) {
		goto clean;
	}
	uint64_t sid = strtoull(ssid, NULL, 16);
	int i;
	for (i = 0; i < NUM_SESSIONS; i++) {
		if (s_sessions[i].id == sid) {
			s_sessions[i].last_used = mg_time();
			ret = &s_sessions[i];
			goto clean;
		}
	}

clean:
	if (ssid != ssid_buf) {
		free(ssid);
	}
	return ret;
}

The ASAN build fails too for the same reasons https://github.com/cesanta/mongoose/pull/1020

1 Like