简体   繁体   English

const char *分配不正确?

[英]Const char* not assigning properly?

I have the following code: 我有以下代码:

#define FROM    "<example@gmail.com>"
#define TO      "<example2@gmail.com>"

const char *payload_text[] = {
    "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
    "To: " TO "\r\n",
    "From: " FROM "(Example User)\r\n",
    "Subject: SMTP TLS example message\r\n",
    "MIME-Version: 1.0\r\n",
    "Content-Type: multipart/mixed; boundary=\"KkK170891tpbkKk__FV_KKKkkkjjwq\"\r\n",
    "\r\n",
    "This is a multipart message in MIME format.",
    "\r\n",
    "--KkK170891tpbkKk__FV_KKKkkkjjwq\r\n",
    "Content-Type: text/plain",
    "\r\n",
    "here goes the text message\r\n",
    "\r\n",
    "--KkK170891tpbkKk__FV_KKKkkkjjwq\r\n",
    "Content-Type: image/jpeg; name=\"test.jpg\"\r\n",
    "Content-Transfer-Encoding: base64\r\n",
    "Content-Disposition: attachment; filename=\"test.jpg\"\r\n",
    "\r\n",
    NULL, /*19*/
    "\r\n",
    "--KkK170891tpbkKk__FV_KKKkkkjjwq--\r\n",
    NULL
};

struct upload_status {
    int lines_read;
};

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) {
  struct upload_status *upload_ctx = (struct upload_status *)userp;
  const char *data;

  if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
    return 0;
  }

  data = payload_text[upload_ctx->lines_read];

  if(data) {
    size_t len = strlen(data);
    memcpy(ptr, data, len);
    upload_ctx->lines_read++;

    return len;
  }

  return 0;
}

std::string readFileToBase64(const char* filename) {
    /* converts binary file to base64 */
}

std::string split76(std::string in) {
    int lines = in.length() / 76;

    for(int i=0;i<lines;i++) {
        in.insert((i+1)*76+i*2, "\r\n");
    }

    return in;
}

int main(void) {
    payload_text[19] = split76(readFileToBase64("C:\\Users\\thrymgjol\\code\\emailtest\\bin\\Release\\test.jpg")).c_str();

  CURL *curl;
  CURLcode res = CURLE_OK;
  struct curl_slist *recipients = NULL;
  struct upload_status upload_ctx;

  upload_ctx.lines_read = 0;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_USERNAME, "example2@gmail.com");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "legitpassword");
    curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587");
    curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);
    recipients = curl_slist_append(recipients, TO);
    curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
    curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

    curl_slist_free_all(recipients);
    curl_easy_cleanup(curl);
  }

  return (int)res;
}

However, when I compile and run, payload_source() stops reading payload_text after the 18th indice. 但是,当我编译并运行时, payload_source()在第18个索引之后停止读取payload_text。 This completely cuts off my attachment, which I assign to payload_text[19]. 这完全切断了我分配给payload_text [19]的附件。 Any ideas on why it does this? 关于为什么这样做的任何想法?

The problem is the value returned from split76 is temporary and is destroyed after the assignment has completed. 问题是从split76返回的值是临时的,并且在分配完成后被销毁。 Any attempt to access it after that results in undefined behavior. 此后进行任何尝试访问都会导致未定义的行为。 If you need to store a pointer to the string buffer you can create local std::string to hold it an ensure it lives long enough to be used. 如果您需要存储指向字符串缓冲区的指针,则可以创建本地std::string来保存它,以确保其寿命足以使用。

std::string encodedFile(split76(readFileToBase64("C:\\Users\\thrymgjol\\code\\emailtest\\bin\\Release\\test.jpg")));
payload_text[19] = encodedFile.c_str();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM