From 638ca0f56c33897601789442ba1414153766daef Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 13 Apr 2024 16:53:29 +0200 Subject: Use asprintf to allocate header instead of fixed array --- ot_http.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/ot_http.c b/ot_http.c index 5b96e6f..2d86ac1 100644 --- a/ot_http.c +++ b/ot_http.c @@ -146,26 +146,23 @@ fprintf(stderr, "http_sendiovecdata sending %d iovec entries found cookie->batch if( iovec_entries ) { - /* Prepare space for http header */ - header = malloc( SUCCESS_HTTP_HEADER_LENGTH + SUCCESS_HTTP_HEADER_LENGTH_CONTENT_ENCODING ); - if( !header ) { - iovec_free( &iovec_entries, &iovector ); - HTTPERROR_500; - } - if( cookie->flag & STRUCT_HTTP_FLAG_GZIP ) encoding = "Content-Encoding: gzip\r\n"; else if( cookie->flag & STRUCT_HTTP_FLAG_BZIP2 ) encoding = "Content-Encoding: bzip2\r\n"; if( !(cookie->flag & STRUCT_HTTP_FLAG_CHUNKED) ) - header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n%sContent-Length: %zd\r\n\r\n", encoding, size ); + header_size = asprintf( &header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n%sContent-Length: %zd\r\n\r\n", encoding, size ); else { if ( !(cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER )) { - header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: application/octet-stream\r\n%sTransfer-Encoding: chunked\r\n\r\n%zx\r\n", encoding, size ); + header_size = asprintf( &header, "HTTP/1.0 200 OK\r\nContent-Type: application/octet-stream\r\n%sTransfer-Encoding: chunked\r\n\r\n%zx\r\n", encoding, size ); cookie->flag |= STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER; } else - header_size = sprintf( header, "%zx\r\n", size ); + header_size = asprintf( &header, "%zx\r\n", size ); + } + if( !header ) { + iovec_free( &iovec_entries, &iovector ); + HTTPERROR_500; } if (!cookie->batch ) { -- cgit v1.2.3