mirror of
https://github.com/curl/curl.git
synced 2026-08-25 12:33:35 +03:00
examples: safer and more proper read callback logic
The same callback code is used in: imap-append.c smtp-authzid.c smtp-mail.c smtp-multi.c smtp-ssl.c smtp-tls.c It should not assume that it can copy full lines into the buffer as it will encourage sloppy coding practices. Instead use byte-wise logic and check/acknowledge the buffer size appropriately. Reported-by: Harry Sintonen Fixes #7330 Closes #7331
This commit is contained in:
parent
07fa74d34a
commit
a37fc62e8b
6 changed files with 139 additions and 149 deletions
|
|
@ -5,7 +5,7 @@
|
|||
* | (__| |_| | _ <| |___
|
||||
* \___|\___/|_| \_\_____|
|
||||
*
|
||||
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
||||
*
|
||||
* This software is licensed as described in the file COPYING, which
|
||||
* you should have received as part of this distribution. The terms
|
||||
|
|
@ -47,41 +47,42 @@
|
|||
#define SENDER_MAIL "Kurt " SENDER_ADDR
|
||||
#define TO_MAIL "A Receiver " TO_ADDR
|
||||
|
||||
static const char *payload_text[] = {
|
||||
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
|
||||
"To: " TO_MAIL "\r\n",
|
||||
"From: " FROM_MAIL "\r\n",
|
||||
"Sender: " SENDER_MAIL "\r\n",
|
||||
static const char *payload_text =
|
||||
"Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n"
|
||||
"To: " TO_MAIL "\r\n"
|
||||
"From: " FROM_MAIL "\r\n"
|
||||
"Sender: " SENDER_MAIL "\r\n"
|
||||
"Message-ID: <dcd7cb36-11db-487a-9f3a-e652a9458efd@"
|
||||
"rfcpedant.example.org>\r\n",
|
||||
"Subject: SMTP example message\r\n",
|
||||
"\r\n", /* empty line to divide headers from body, see RFC5322 */
|
||||
"The body of the message starts here.\r\n",
|
||||
"\r\n",
|
||||
"It could be a lot of lines, could be MIME encoded, whatever.\r\n",
|
||||
"Check RFC5322.\r\n",
|
||||
NULL
|
||||
};
|
||||
"rfcpedant.example.org>\r\n"
|
||||
"Subject: SMTP example message\r\n"
|
||||
"\r\n" /* empty line to divide headers from body, see RFC5322 */
|
||||
"The body of the message starts here.\r\n"
|
||||
"\r\n"
|
||||
"It could be a lot of lines, could be MIME encoded, whatever.\r\n"
|
||||
"Check RFC5322.\r\n";
|
||||
|
||||
struct upload_status {
|
||||
int lines_read;
|
||||
size_t bytes_read;
|
||||
};
|
||||
|
||||
static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
struct upload_status *upload_ctx = (struct upload_status *)userp;
|
||||
const char *data;
|
||||
size_t room = size * nmemb;
|
||||
|
||||
if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
data = payload_text[upload_ctx->lines_read];
|
||||
data = &payload_text[upload_ctx->bytes_read];
|
||||
|
||||
if(data) {
|
||||
size_t len = strlen(data);
|
||||
if(room < len)
|
||||
len = room;
|
||||
memcpy(ptr, data, len);
|
||||
upload_ctx->lines_read++;
|
||||
upload_ctx->bytes_read += len;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
|
@ -94,9 +95,7 @@ int main(void)
|
|||
CURL *curl;
|
||||
CURLcode res = CURLE_OK;
|
||||
struct curl_slist *recipients = NULL;
|
||||
struct upload_status upload_ctx;
|
||||
|
||||
upload_ctx.lines_read = 0;
|
||||
struct upload_status upload_ctx = { 0 };
|
||||
|
||||
curl = curl_easy_init();
|
||||
if(curl) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue