Bug Summary

File:src/kits/network/libnetapi/DataRequest.cpp
Location:line 103, column 3
Description:Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'

Annotated Source Code

1/*
2 * Copyright 2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Adrien Destugues, pulkomandy@pulkomandy.tk
7 */
8
9
10#include "DataRequest.h"
11
12#include <HttpAuthentication.h>
13#include <mail_encoding.h>
14#include <stdio.h>
15
16
17BDataRequest::BDataRequest(const BUrl& url, BUrlProtocolListener* listener,
18 BUrlContext* context)
19 : BUrlRequest(url, listener, context, "data URL parser", "data"),
20 fResult()
21{
22 fResult.SetContentType("text/plain");
23}
24
25
26const BUrlResult&
27BDataRequest::Result() const
28{
29 return fResult;
30}
31
32
33status_t
34BDataRequest::_ProtocolLoop()
35{
36 BString mimeType;
37 BString charset;
38 const char* payload;
39 size_t length;
40 bool isBase64 = false;
41
42 fUrl.UrlDecode(true);
43 BString data = fUrl.Path();
44 int separatorPosition = data.FindFirst(',');
45
46 if (fListener != NULL__null)
1
Taking false branch
47 fListener->ConnectionOpened(this);
48
49 if (separatorPosition >= 0) {
2
Assuming 'separatorPosition' is >= 0
3
Taking true branch
50 BString meta = data;
51 meta.Truncate(separatorPosition);
52 data.Remove(0, separatorPosition + 1);
53
54 int pos = 0;
55 while(meta.Length() > 0)
4
Loop condition is true. Entering loop body
8
Loop condition is false. Execution continues on line 78
56 {
57 // Extract next parameter
58 pos = meta.FindFirst(';', pos);
59
60 BString parameter = meta;
61 if(pos >= 0) {
5
Assuming 'pos' is < 0
6
Taking false branch
62 parameter.Truncate(pos);
63 meta.Remove(0, pos+1);
64 } else
65 meta.Truncate(0);
66
67 // Interpret the parameter
68 if(parameter == "base64") {
7
Taking true branch
69 isBase64 = true;
70 } else if(parameter.FindFirst("charset=") == 0) {
71 charset = parameter;
72 } else {
73 // Must be the MIME type
74 mimeType = parameter;
75 }
76 }
77
78 if (charset.Length() > 0)
9
Taking false branch
79 mimeType << ";" << charset;
80 fResult.SetContentType(mimeType);
81 }
82
83 if (isBase64) {
10
Taking true branch
84 char* buffer = new char[data.Length() * 4 / 3];
11
Memory is allocated
85 payload = buffer;
86 // payload must be a const char* so we can assign data.String() to
87 // it below, but decode_64 modifies buffer.
88 length = decode_base64(buffer, data.String(), data.Length());
89 } else {
90 payload = data.String();
91 length = data.Length();
92 }
93
94 fResult.SetLength(length);
95
96 if (fListener != NULL__null) {
12
Taking false branch
97 fListener->DownloadProgress(this, length, length);
98 if (length > 0)
99 fListener->DataReceived(this, payload, length);
100 }
101
102 if (isBase64)
13
Taking true branch
103 delete payload;
14
Memory allocated by 'new[]' should be deallocated by 'delete[]', not 'delete'
104
105 return B_PROT_SUCCESS;
106}