LoadStoreError while concatenating strings

dmitrij999
Posts: 71
Joined: Sat Mar 02, 2019 8:06 pm

LoadStoreError while concatenating strings

Postby dmitrij999 » Sun Mar 24, 2019 7:00 am

  • Hardware: ESP32-WROOM
  • ESP-IDF: Latest
I want to return file list in SPIFFS in browser on address http://<esp32.ip>/fs_list but I get the following error:

Code: Select all

File list has been requested...
Beginning of serialization...
5 files has been found
SPIFFS: free 785 KB of 934 KB
-----------------------------------

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x4000c52a  PS      : 0x00060e30  A0      : 0x80108771  A1      : 0x3ffee730  
A2      : 0x3ffee750  A3      : 0x00000011  A4      : 0x00000009  A5      : 0x3ffee770  
A6      : 0x00000000  A7      : 0x3a22657a  A8      : 0x00000000  A9      : 0x3ffee770  
A10     : 0x00000011  A11     : 0x00000001  A12     : 0x800949ac  A13     : 0x3ffbee90  
A14     : 0x00000003  A15     : 0x00060023  SAR     : 0x00000008  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000011  LBEG    : 0x4000c349  LEND    : 0x4000c36b  LCOUNT  : 0xffffffff  

ELF file SHA256: 46648d301515cb32c0ffb99f1bacfd742fc5c2b20ef8b168dda85277fef54312

Backtrace: 0x4000c52a:0x3ffee730 0x4010876e:0x3ffee750 0x401a77b7:0x3ffee790 0x401a7e99:0x3ffee7d0 0x401a7f2d:0x3ffee860 0x401a8418:0x3ffee880 0x401a6932:0x3ffee8a0 0x401a69a4:0x3ffee8e0 0x40094e05:0x3ffee900
0x4010876e: http_list_files at /home/dmitrij999/esp/a2dp_sink/main/main.c:711 (discriminator 9)

0x401a77b7: httpd_uri at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_uri.c:311

0x401a7e99: httpd_parse_req at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_parse.c:699

0x401a7f2d: httpd_req_new at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_parse.c:699

0x401a8418: httpd_sess_process at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_sess.c:326

0x401a6932: httpd_server at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_main.c:113

0x401a69a4: httpd_thread at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_main.c:113

0x40094e05: vPortTaskWrapper at /home/dmitrij999/esp/esp-idf/components/freertos/port.c:403
while concatenating strings. I tried both with memory allocation and without it.

Here is the code stub:

Code: Select all

esp_err_t http_list_files(httpd_req_t *req) {
    spiffs_file *f_list;

    char * json_string = NULL;
    int char_ptr = 0;

    //string json;

    char buf;
    int ret;
    printf("File list has been requested...\n");
    /*
    if ((ret = httpd_req_recv(req, &buf, 1)) <= 0) {
        if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
            httpd_resp_send_408(req);
        }
        return ESP_FAIL;
    }
    */
    printf("Beginning of serialization...\n");

    int files = count_files("/spiffs", NULL);

    json_string = malloc(files*720);
    if (json_string == NULL) {
        printf("No memory!");
        return ESP_FAIL;
    }

    printf("%d files has been found\n", files);

    //strcat(json_string, "[");

    f_list = file_list("/spiffs", NULL);
    
    
    if (f_list == NULL) {
        httpd_resp_send_500(req);
        //httpd_resp_set_status(req, "502");
        const char *resp = "Internal Server Error";
        httpd_resp_send(req, resp, strlen(resp));
        return ESP_FAIL;
    }
    /*

    for (int i = 0; i < files; i++) {
        printf("%d  %d  %s\r\n",
          f_list[i].type,
          f_list[i].size,
          f_list[i].path
          //tbuffer,
          //ent->d_name
      );
    }
    */
    //printf("Array size: %d", sizeof(json_string));
    
    //strcat(json_string, "[");
    json_string = "[";
    myStrcat(json_string, "{}]");
    /*
    for (int i = 0; i < files-1; i++) {
        json_string = json_string + "{\"file\":\"";
        strcat(json_string, f_list[i].path);
        strcat(json_string, "\",\"size\":");
        strcat(json_string, f_list[i].size);
        strcat(json_string, "},");
        printf("%d  %d  %s\r\n",
          f_list[i].type,
          f_list[i].size,
          f_list[i].path
          //tbuffer,
          //ent->d_name
      );
    }
    strcat(json_string, "{\"file\":\"");
    strcat(json_string, f_list[files].path);
    strcat(json_string, "\",\"size\":");
    strcat(json_string, f_list[files].size);
    strcat(json_string, "}]\0");
    */
    printf(json_string);
    

    //char *sarylosk = "{\"name\":\"sarylosk\"}";
    httpd_resp_set_type(req, "application/json");
    httpd_resp_send(req, json_string, strlen(json_string));

    free(f_list);
    free(json_string);

    return ESP_OK;
}
What do I do wrong?


dmitrij999
Posts: 71
Joined: Sat Mar 02, 2019 8:06 pm

Re: LoadStoreError while concatenating strings

Postby dmitrij999 » Sun Mar 24, 2019 2:10 pm

I changed my code, and now I get LoadProhibited error

Code:

Code: Select all

esp_err_t http_list_files(httpd_req_t *req) {
    spiffs_file *f_list;

    char json_string[2048];
    int char_ptr = 0;

    //string json;

    char buf;
    int ret;
    printf("File list has been requested...\n");
    /*
    if ((ret = httpd_req_recv(req, &buf, 1)) <= 0) {
        if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
            httpd_resp_send_408(req);
        }
        return ESP_FAIL;
    }
    */
    printf("Beginning of serialization...\n");

    int files = count_files("/spiffs", NULL);

    //json_string = malloc(files*720);
    if (json_string == NULL) {
        printf("No memory!");
        return ESP_FAIL;
    }

    printf("%d files has been found\n", files);

    //strcat(json_string, "[");

    f_list = file_list("/spiffs", NULL);
    
    
    if (f_list == NULL) {
        httpd_resp_send_500(req);
        //httpd_resp_set_status(req, "502");
        const char *resp = "Internal Server Error";
        httpd_resp_send(req, resp, strlen(resp));
        return ESP_FAIL;
    }
    

    json_string[0] = "\0";
    //myStrcat(json_string, "{}]");
    strcpy(json_string, "[");
    for (int i = 0; i < files-1; i++) {
        strcat(json_string, "{\"file\":\"");
        strcat(json_string, f_list[i].path);
        strcat(json_string, "\",\"size\":");
        strcat(json_string, f_list[i].size);
        strcat(json_string, "},");
        printf("%d  %d  %s\r\n",
          f_list[i].type,
          f_list[i].size,
          f_list[i].path
          //tbuffer,
          //ent->d_name
      );
    }
    strcat(json_string, "{\"file\":\"");
    strcat(json_string, f_list[files].path);
    strcat(json_string, "\",\"size\":");
    strcat(json_string, f_list[files].size);
    strcat(json_string, "}]\0");
    
    printf(json_string);
    

    //char *sarylosk = "{\"name\":\"sarylosk\"}";
    httpd_resp_set_type(req, "application/json");
    httpd_resp_send(req, json_string, strlen(json_string));

    free(f_list);
    //free(json_string);

    return ESP_OK;
}
Error log

Code: Select all

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x4000c52a  PS      : 0x00060330  A0      : 0x8010874c  A1      : 0x3fff32a0  
A2      : 0x3fff32c0  A3      : 0x00000011  A4      : 0x0000000a  A5      : 0x3fff32de  
A6      : 0x0000003a  A7      : 0x22657a69  A8      : 0x00000000  A9      : 0x3fff32dd  
A10     : 0x00000011  A11     : 0x3fff32d4  A12     : 0x000003a6  A13     : 0x3ffba808  
A14     : 0x3fff3208  A15     : 0x00000010  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000011  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0xffffffff  

ELF file SHA256: c010f80b65f6f17d99951705b4de6045a3def3aa3e27e3661f200ec765871a87

Backtrace: 0x4000c52a:0x3fff32a0 0x40108749:0x3fff32c0 0x40195f83:0x3fff3af0 0x40196665:0x3fff3b30 0x401966f9:0x3fff3bc0 0x40196be4:0x3fff3be0 0x401950fe:0x3fff3c00 0x40195170:0x3fff3c40 0x40094e05:0x3fff3c60
0x40108749: http_list_files at /home/dmitrij999/esp/a2dp_sink/main/main.c:732 (discriminator 9)

0x40195f83: httpd_uri at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_uri.c:311

0x40196665: httpd_parse_req at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_parse.c:699

0x401966f9: httpd_req_new at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_parse.c:699

0x40196be4: httpd_sess_process at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_sess.c:326

0x401950fe: httpd_server at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_main.c:113

0x40195170: httpd_thread at /home/dmitrij999/esp/esp-idf/components/esp_http_server/src/httpd_main.c:113

0x40094e05: vPortTaskWrapper at /home/dmitrij999/esp/esp-idf/components/freertos/port.c:403


WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: LoadStoreError while concatenating strings

Postby WiFive » Sun Mar 24, 2019 6:16 pm

You also moved json_string to a stack variable so your task better have a large stack.

dmitrij999
Posts: 71
Joined: Sat Mar 02, 2019 8:06 pm

Re: LoadStoreError while concatenating strings

Postby dmitrij999 » Sun Mar 24, 2019 6:22 pm

WiFive wrote:
Sun Mar 24, 2019 6:16 pm
You also moved json_string to a stack variable so your task better have a large stack.
It has 32768 bytes stack

Who is online

Users browsing this forum: No registered users and 184 guests