From 482f6340b96a0a5ab5659ad7da95df32daf8f5e7 Mon Sep 17 00:00:00 2001 From: hwqchi Date: Wed, 21 Jun 2023 04:42:03 +0800 Subject: [PATCH] esp_http_client: Fix unsigned integer overflow Fix unsigned integer overflow when `content_length` is -1. The proposed change is to modify the format specifier for content_length from PRIu64 to PRId64, which will print the signed value as a 64-bit integer instead. --- .../main/esp_http_client_example.c | 62 +++++++++---------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/examples/protocols/esp_http_client/main/esp_http_client_example.c b/examples/protocols/esp_http_client/main/esp_http_client_example.c index 4209e8f55b..02b880a722 100644 --- a/examples/protocols/esp_http_client/main/esp_http_client_example.c +++ b/examples/protocols/esp_http_client/main/esp_http_client_example.c @@ -156,7 +156,7 @@ static void http_rest_with_url(void) // GET esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -172,7 +172,7 @@ static void http_rest_with_url(void) esp_http_client_set_post_field(client, post_data, strlen(post_data)); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -184,7 +184,7 @@ static void http_rest_with_url(void) esp_http_client_set_method(client, HTTP_METHOD_PUT); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -197,7 +197,7 @@ static void http_rest_with_url(void) esp_http_client_set_post_field(client, NULL, 0); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -209,7 +209,7 @@ static void http_rest_with_url(void) esp_http_client_set_method(client, HTTP_METHOD_DELETE); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -221,7 +221,7 @@ static void http_rest_with_url(void) esp_http_client_set_method(client, HTTP_METHOD_HEAD); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -244,7 +244,7 @@ static void http_rest_with_hostname_path(void) // GET esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -258,7 +258,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_post_field(client, post_data, strlen(post_data)); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -270,7 +270,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_method(client, HTTP_METHOD_PUT); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP PUT Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -283,7 +283,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_post_field(client, NULL, 0); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP PATCH Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -295,7 +295,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_method(client, HTTP_METHOD_DELETE); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP DELETE Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -307,7 +307,7 @@ static void http_rest_with_hostname_path(void) esp_http_client_set_method(client, HTTP_METHOD_HEAD); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP HEAD Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -337,7 +337,7 @@ static void http_auth_basic(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Basic Auth Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Basic Auth Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -356,7 +356,7 @@ static void http_auth_basic_redirect(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Basic Auth redirect Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Basic Auth redirect Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -377,7 +377,7 @@ static void http_auth_digest(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Digest Auth Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Digest Auth Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -399,7 +399,7 @@ static void https_with_url(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -422,7 +422,7 @@ static void https_with_hostname_path(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -451,7 +451,7 @@ static void http_encoded_query(void) esp_http_client_handle_t client = esp_http_client_init(&config); esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -469,7 +469,7 @@ static void http_relative_redirect(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Relative path redirect Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Relative path redirect Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -488,7 +488,7 @@ static void http_absolute_redirect(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Absolute path redirect Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Absolute path redirect Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -508,7 +508,7 @@ static void http_absolute_redirect_manual(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Absolute path redirect (manual) Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Absolute path redirect (manual) Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -528,7 +528,7 @@ static void http_redirect_to_https(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP redirect to HTTPS Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP redirect to HTTPS Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -548,7 +548,7 @@ static void http_download_chunk(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP chunk encoding Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP chunk encoding Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -584,7 +584,7 @@ static void http_perform_as_stream_reader(void) buffer[read_len] = 0; ESP_LOGD(TAG, "read_len = %d", read_len); } - ESP_LOGI(TAG, "HTTP Stream reader Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Stream reader Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); esp_http_client_close(client); @@ -616,7 +616,7 @@ static void https_async(void) } } if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -635,7 +635,7 @@ static void https_with_invalid_url(void) esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTPS Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -671,7 +671,7 @@ static void http_native_request(void) } else { int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); if (data_read >= 0) { - ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); ESP_LOG_BUFFER_HEX(TAG, output_buffer, data_read); @@ -701,7 +701,7 @@ static void http_native_request(void) } else { int data_read = esp_http_client_read_response(client, output_buffer, MAX_HTTP_OUTPUT_BUFFER); if (data_read >= 0) { - ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); ESP_LOG_BUFFER_HEX(TAG, output_buffer, strlen(output_buffer)); @@ -727,7 +727,7 @@ static void http_partial_download(void) esp_http_client_set_header(client, "Range", "bytes=10-"); esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -738,7 +738,7 @@ static void http_partial_download(void) esp_http_client_set_header(client, "Range", "bytes=-10"); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else { @@ -749,7 +749,7 @@ static void http_partial_download(void) esp_http_client_set_header(client, "Range", "bytes=11-20"); err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRIu64, + ESP_LOGI(TAG, "HTTP Status = %d, content_length = %"PRId64, esp_http_client_get_status_code(client), esp_http_client_get_content_length(client)); } else {