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 <waddlesplash@gmail.com>
This commit is contained in:
Leorize 2020-07-15 23:49:44 -05:00 committed by waddlesplash
parent e1de8e8c21
commit 2056d60ff1

View File

@ -9,6 +9,7 @@
#include "DataRequest.h"
#include <AutoDeleter.h>
#include <HttpAuthentication.h>
#include <mail_encoding.h>
#include <stdio.h>
@ -88,6 +89,7 @@ BDataRequest::_ProtocolLoop()
}
ArrayDeleter<char> 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;
}