Почему я получаю ошибку памяти из-за libcurl? - PullRequest
0 голосов
/ 19 июня 2020

У меня есть фрагмент кода, который ведет себя странно.

Я получаю ошибку «malloc_consolidate (): неверный размер фрагмента», но только в третий раз, когда я запускаю функцию.

Еще два раза я запускал его, он работает нормально.

Я использую aarch linux на процессоре arm и запускаю openSSL с движком TPM.

Есть ли у кого-нибудь подсказка что могло вызвать эту ошибку?

Я действительно удивлен, так как сам не вызываю malloc_consolidate.

Заранее благодарим за любой ввод.

struct FtpFile {
  const char *filename;
  FILE *stream;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out = (struct FtpFile *)stream;
  if(!out->stream) {
    /* open file for writing */ 
    out->stream = fopen(out->filename, "wb");
    if(!out->stream)
      return -1; /* failure, can't open file to write */ 
  }
  return fwrite(buffer, size, nmemb, out->stream);
}

//int ftps_get(void){

int ftps_get(char * URL){
  CURL *curl;
  CURLcode res;
  char filename[20] = "downloaded.txt";
  struct FtpFile ftpfile = {
    filename,       //"downloaded.txt", /* name to store the file as if successful */ 
    NULL
  };
  char ftps_private_seed[100] = "certs/ftps_filezilla/test_privatekey_device.key"; //rename
  char ftps_cert[100] = "certs/ftps_filezilla/test_device_cert.crt";
  //char CA_path[100] = "certs/CA/filezilla.crt";
  curl_global_init(CURL_GLOBAL_SSL);

  curl = curl_easy_init();
  if(curl) {
    /*
     * FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if
     * you want to do the rarer kind of transfers: implicit.
     */ 
    //curl_easy_setopt(curl, CURLOPT_URL, "ftp://demo:password@192.168.1.79:21/readme.txt");
    curl_easy_setopt(curl, CURLOPT_URL, URL);

    /* Define our callback to get called when there's data to be written */ 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
    /* Set a pointer to our struct to pass to the callback */ 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

    /* We activate SSL and we require it for both control and data */ 
    curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);

    /* Switch on full protocol/debug output */ 
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

    //engine
    curl_easy_setopt(curl, CURLOPT_SSLENGINE, "tpm2tss");

    //keys
    curl_easy_setopt(curl, CURLOPT_SSLCERT, ftps_cert);
    curl_easy_setopt(curl, CURLOPT_SSLKEY, ftps_private_seed);

    //insecure no verify CA
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

    //CA path (directory) CURLOPT_CAPATH
    //curl_easy_setopt(curl, CURLOPT_CAINFO , CA_path ); //try CURLOPT_CAINFO if need specific file 

    //TCP keep alive
    curl_easy_setopt(curl, CURLOPT_TCP_KEEPALIVE, 1L);

    //time out  (seconds)
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 300);

    res = curl_easy_perform(curl);

    /* always cleanup */ 
    curl_easy_cleanup(curl);

    if(CURLE_OK != res) {
      /* we failed */ 
      fprintf(stderr, "curl told us %d\n", res);
    }
  }

  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */ 

  curl_global_cleanup();

  return 0;
}
...