From 2056d60ff1cf632ae3777bbacdd15326f741adfa Mon Sep 17 00:00:00 2001 From: Leorize Date: Wed, 15 Jul 2020 23:49:44 -0500 Subject: [PATCH] DataRequest: Use ArrayDeleter to manage the temporary buffer This simplify the clean up code path and make sure that the temporary buffer will always be freed on exit. Change-Id: I70c1a25bfa66c4f4a96dd3d0af3765a67e305fb2 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3074 Reviewed-by: waddlesplash --- src/kits/network/libnetapi/DataRequest.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/kits/network/libnetapi/DataRequest.cpp b/src/kits/network/libnetapi/DataRequest.cpp index 6afd33c10b..a9ece68851 100644 --- a/src/kits/network/libnetapi/DataRequest.cpp +++ b/src/kits/network/libnetapi/DataRequest.cpp @@ -9,6 +9,7 @@ #include "DataRequest.h" +#include #include #include #include @@ -88,6 +89,7 @@ BDataRequest::_ProtocolLoop() } + ArrayDeleter buffer; if (isBase64) { // Check that the base64 data is properly padded (we process characters // by groups of 4 and there must not be stray chars at the end as @@ -95,18 +97,17 @@ BDataRequest::_ProtocolLoop() if (data.Length() & 3) return B_BAD_DATA; - char* buffer = new char[data.Length() * 3 / 4]; - payload = buffer; + buffer.SetTo(new char[data.Length() * 3 / 4]); + payload = buffer.Get(); // payload must be a const char* so we can assign data.String() to // it below, but decode_64 modifies buffer. - length = decode_base64(buffer, data.String(), data.Length()); + length = decode_base64(buffer.Get(), data.String(), data.Length()); // There may be some padding at the end of the base64 stream. This // prevents us from computing the exact length we should get, so allow // for some error margin. if (length > data.Length() * 3 / 4 || length < data.Length() * 3 / 4 - 3) { - delete[] buffer; return B_BAD_DATA; } } else { @@ -124,8 +125,5 @@ BDataRequest::_ProtocolLoop() } } - if (isBase64) - delete[] payload; - return B_OK; }