netconn_write issue and sprintf issue esp32

blavoie
Posts: 16
Joined: Sat Jan 21, 2017 6:41 pm

netconn_write issue and sprintf issue esp32

Postby blavoie » Tue Feb 21, 2017 9:03 pm

I'm working on a webserver application and I'm print out a webpage with dynamic variables and can't seem to get it to work because of various issues.

In my first attempt, I tried writing out a page line by line with code that looked like this. The problem is, the text didn't render correctly on the webpage. Characters were out of order and it wasn't exactly how I sent it.

strcpy(strBuffer, "function updatePageID(){\n");
netconn_write(conn, strBuffer, sizeof(strBuffer)-1, NETCONN_NOCOPY);
sprintf(strBuffer," document.getElementById('SRT').checked=%s;\n",http_util_truefalse_str_string(p->bEnableSRT));
netconn_write(conn, strBuffer, sizeof(strBuffer)-1, NETCONN_NOCOPY);
strcpy(strBuffer, "}\n");
netconn_write(conn, strBuffer, sizeof(strBuffer)-1, NETCONN_NOCOPY);

Next I tried creating a single buffer and that is giving a core dump. I took out the netconn_write and replaced it with the printf("%s", strBuffer); just to see if netconn had an issue. It still core dumps with the printf, there is a problem with the with the sprintf(

I'm hoping someone will look at this and have an idea on what I can try next.

char strBuffer [1024];

memset(strBuffer,0,(sizeof(strBuffer)));

strcpy(strBuffer, "function updatePageID(){\n");
sprintf(strBuffer + strlen(strBuffer)," document.getElementById('SRT').checked=%s;\n",http_util_truefalse_str_string(p->bEnableSRT));
strcpy(strBuffer + strlen(strBuffer), "}\n");

netconn_write(conn, strBuffer, sizeof(strBuffer)-1, NETCONN_NOCOPY);

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: netconn_write issue and sprintf issue esp32

Postby ESP_Angus » Wed Feb 22, 2017 1:43 am

A couple of things with netconn_write that probably explain the bug:

- NETCONN_NOCOPY indicates that the data is stable for the lifetime of the transmission. The underlying transmission happens in a different task, so if you use this function then you can't change the contents of the buffer which is passed. Use NETCONN_COPY for the situation you have here, where the same buffer is being reused multiple times with different data. NETCONN_NOCOPY is really only good for things like string literals, or other data where the buffer contents will never change.
- You're passing sizeof(strBuffer) to netconn_write, which is 1024. You probably only want to write strlen(strBuffer) bytes, otherwise the other end is going to receive a lot of NUL bytes.

I'm not sure why the sprintf is crashing (possibly a stack overflow), but the above probably explains the problems with the web client.


Angus

blavoie
Posts: 16
Joined: Sat Jan 21, 2017 6:41 pm

Re: netconn_write issue and sprintf issue esp32

Postby blavoie » Wed Feb 22, 2017 2:27 pm

You're right, strlen is what should be used instead of sizeof(), cut and paste error. As many times as I looked at it, I was thinking I was seeing strlen(). Sorry to take up the bandwidth on the post.

Thank you so much for the help.

Who is online

Users browsing this forum: Baidu [Spider] and 107 guests