Skip to content

Commit 6925982

Browse files
committed
replace new by new (std::nothrow), remove arduino_new
1 parent 5b3d290 commit 6925982

File tree

45 files changed

+207
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+207
-167
lines changed

cores/esp8266/Esp.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -740,17 +740,17 @@ String EspClass::getSketchMD5()
740740
}
741741
uint32_t lengthLeft = getSketchSize();
742742
const size_t bufSize = 512;
743-
std::unique_ptr<uint8_t[]> buf(new uint8_t[bufSize]);
743+
std::unique_ptr<uint8_t[]> buf(new (std::nothrow) uint8_t[bufSize]);
744744
uint32_t offset = 0;
745745
if(!buf.get()) {
746-
return String();
746+
return emptyString;
747747
}
748748
MD5Builder md5;
749749
md5.begin();
750750
while( lengthLeft > 0) {
751751
size_t readBytes = (lengthLeft < bufSize) ? lengthLeft : bufSize;
752752
if (!flashRead(offset, reinterpret_cast<uint32_t*>(buf.get()), (readBytes + 3) & ~3)) {
753-
return String();
753+
return emptyString;
754754
}
755755
md5.add(buf.get(), readBytes);
756756
lengthLeft -= readBytes;

cores/esp8266/FunctionalInterrupt.cpp

+29-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ typedef void (*voidFuncPtr)(void);
77
typedef void (*voidFuncPtrArg)(void*);
88

99
// Helper functions for Functional interrupt routines
10-
extern "C" void __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtr userFunc, void*fp, int mode, bool functional);
10+
extern "C" bool __attachInterruptFunctionalArg(uint8_t pin, voidFuncPtr userFunc, void*fp, int mode, bool functional);
1111

1212

1313
void ICACHE_RAM_ATTR interruptFunctional(void* arg)
@@ -34,32 +34,52 @@ extern "C"
3434
}
3535
}
3636

37-
void attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode)
37+
bool attachInterrupt(uint8_t pin, std::function<void(void)> intRoutine, int mode)
3838
{
3939
// use the local interrupt routine which takes the ArgStructure as argument
4040

4141
InterruptInfo* ii = nullptr;
4242

43-
FunctionInfo* fi = new FunctionInfo;
43+
FunctionInfo* fi = new (std::nothrow) FunctionInfo;
44+
if (fi == nullptr)
45+
return false;
4446
fi->reqFunction = intRoutine;
4547

46-
ArgStructure* as = new ArgStructure;
48+
ArgStructure* as = new (std::nothrow) ArgStructure;
49+
if (as == nullptr)
50+
{
51+
delete(fi);
52+
return false;
53+
}
4754
as->interruptInfo = ii;
4855
as->functionInfo = fi;
4956

50-
__attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
57+
return __attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
5158
}
5259

53-
void attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
60+
bool attachScheduledInterrupt(uint8_t pin, std::function<void(InterruptInfo)> scheduledIntRoutine, int mode)
5461
{
55-
InterruptInfo* ii = new InterruptInfo;
62+
InterruptInfo* ii = new (std::nothrow) InterruptInfo;
63+
if (ii == nullptr)
64+
return false;
5665

5766
FunctionInfo* fi = new FunctionInfo;
67+
if (fi == nullptr)
68+
{
69+
delete ii;
70+
return false;
71+
}
5872
fi->reqScheduledFunction = scheduledIntRoutine;
5973

60-
ArgStructure* as = new ArgStructure;
74+
ArgStructure* as = new (std::nothrow) ArgStructure;
75+
if (as == nullptr)
76+
{
77+
delete ii;
78+
delete fi;
79+
return false;
80+
}
6181
as->interruptInfo = ii;
6282
as->functionInfo = fi;
6383

64-
__attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
84+
return __attachInterruptFunctionalArg(pin, (voidFuncPtr)interruptFunctional, as, mode, true);
6585
}

cores/esp8266/Print.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ size_t Print::printf(const char *format, ...) {
6363
size_t len = vsnprintf(temp, sizeof(temp), format, arg);
6464
va_end(arg);
6565
if (len > sizeof(temp) - 1) {
66-
buffer = new char[len + 1];
66+
buffer = new (std::nothrow) char[len + 1];
6767
if (!buffer) {
6868
return 0;
6969
}
@@ -86,7 +86,7 @@ size_t Print::printf_P(PGM_P format, ...) {
8686
size_t len = vsnprintf_P(temp, sizeof(temp), format, arg);
8787
va_end(arg);
8888
if (len > sizeof(temp) - 1) {
89-
buffer = new char[len + 1];
89+
buffer = new (std::nothrow) char[len + 1];
9090
if (!buffer) {
9191
return 0;
9292
}

cores/esp8266/Updater.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
180180
} else {
181181
_bufferSize = 256;
182182
}
183-
_buffer = new uint8_t[_bufferSize];
183+
_buffer = new (std::nothrow) uint8_t[_bufferSize];
184184
_command = command;
185185

186186
#ifdef DEBUG_UPDATER

cores/esp8266/cbuf.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ size_t cbuf::resize(size_t newSize) {
4343
return _size;
4444
}
4545

46-
char *newbuf = new char[newSize];
46+
char *newbuf = new (std::nothrow) char[newSize];
4747
char *oldbuf = _buf;
4848

4949
if(!newbuf) {

cores/esp8266/core_esp8266_features.h

-29
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,6 @@
3636
#include <stddef.h> // size_t
3737
#include <stdint.h>
3838

39-
#ifdef __cplusplus
40-
41-
namespace arduino
42-
{
43-
extern "C++"
44-
template <typename T, typename ...TConstructorArgs>
45-
T* new0 (size_t n, TConstructorArgs... TconstructorArgs)
46-
{
47-
// n==0: single allocation, otherwise it is an array
48-
size_t offset = n? sizeof(size_t): 0;
49-
size_t arraysize = n? n: 1;
50-
T* ptr = (T*)malloc(offset + (arraysize * sizeof(T)));
51-
if (ptr)
52-
{
53-
if (n)
54-
*(size_t*)(ptr) = n;
55-
for (size_t i = 0; i < arraysize; i++)
56-
new (ptr + offset + i * sizeof(T)) T(TconstructorArgs...);
57-
return ptr + offset;
58-
}
59-
return nullptr;
60-
}
61-
}
62-
63-
#define arduino_new(Type, ...) arduino::new0<Type>(0, ##__VA_ARGS__)
64-
#define arduino_newarray(Type, n, ...) arduino::new0<Type>(n, ##__VA_ARGS__)
65-
66-
#endif // __cplusplus
67-
6839
#ifndef __STRINGIFY
6940
#define __STRINGIFY(a) #a
7041
#endif

doc/reference.rst

-33
Original file line numberDiff line numberDiff line change
@@ -323,36 +323,3 @@ C++
323323
This assures correct behavior, including handling of all subobjects, which guarantees stability.
324324

325325
History: `#6269 <https://github.com/esp8266/Arduino/issues/6269>`__ `#6309 <https://github.com/esp8266/Arduino/pull/6309>`__ `#6312 <https://github.com/esp8266/Arduino/pull/6312>`__
326-
327-
- New optional allocator ``arduino_new``
328-
329-
A new optional global allocator is introduced with a different semantic:
330-
331-
- never throws exceptions on oom
332-
333-
- never calls constructors on oom
334-
335-
- returns nullptr on oom
336-
337-
It is similar to arduino ``new`` semantic without side effects
338-
(except when parent constructors, or member constructors use ``new``).
339-
340-
Syntax is slightly different, the following shows the different usages:
341-
342-
.. code:: cpp
343-
344-
// with new:
345-
346-
SomeClass* sc = new SomeClass(arg1, arg2, ...);
347-
delete sc;
348-
349-
SomeClass* scs = new SomeClass[42];
350-
delete [] scs;
351-
352-
// with arduino_new:
353-
354-
SomeClass* sc = arduino_new(SomeClass, arg1, arg2, ...);
355-
delete sc;
356-
357-
SomeClass* scs = arduino_newarray(SomeClass, 42);
358-
delete [] scs;

libraries/ArduinoOTA/ArduinoOTA.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void ArduinoOTAClass::setRebootOnSuccess(bool reboot){
106106
_rebootOnSuccess = reboot;
107107
}
108108

109-
void ArduinoOTAClass::begin(bool useMDNS) {
109+
bool ArduinoOTAClass::begin(bool useMDNS) {
110110
if (_initialized)
111111
return;
112112

@@ -126,11 +126,13 @@ void ArduinoOTAClass::begin(bool useMDNS) {
126126
_udp_ota = 0;
127127
}
128128

129-
_udp_ota = new UdpContext;
129+
_udp_ota = new (std::nothrow) UdpContext;
130+
if (_udp_ota == nullptr)
131+
return false;
130132
_udp_ota->ref();
131133

132134
if(!_udp_ota->listen(IP_ADDR_ANY, _port))
133-
return;
135+
return false;
134136
_udp_ota->onRx(std::bind(&ArduinoOTAClass::_onRx, this));
135137

136138
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
@@ -149,6 +151,7 @@ void ArduinoOTAClass::begin(bool useMDNS) {
149151
#ifdef OTA_DEBUG
150152
OTA_DEBUG.printf("OTA server at: %s.local:%u\n", _hostname.c_str(), _port);
151153
#endif
154+
return true;
152155
}
153156

154157
int ArduinoOTAClass::parseInt(){

libraries/DNSServer/src/DNSServer.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ void DNSServer::processNextRequest()
178178
return;
179179

180180
std::unique_ptr<uint8_t[]> buffer(new (std::nothrow) uint8_t[currentPacketSize]);
181-
182-
if (buffer == NULL)
181+
if (buffer == nullptr)
183182
return;
184183

185184
_udp.read(buffer.get(), currentPacketSize);

libraries/EEPROM/EEPROM.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ void EEPROMClass::begin(size_t size) {
6464
//In case begin() is called a 2nd+ time, don't reallocate if size is the same
6565
if(_data && size != _size) {
6666
delete[] _data;
67-
_data = new uint8_t[size];
67+
_data = new (std::nothrow) uint8_t[size];
6868
} else if(!_data) {
69-
_data = new uint8_t[size];
69+
_data = new (std::nothrow) uint8_t[size];
70+
}
71+
if (_data == nullptr) {
72+
return;
7073
}
7174

7275
_size = size;

libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ void loop() {
4141
// wait for WiFi connection
4242
if ((WiFiMulti.run() == WL_CONNECTED)) {
4343

44-
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
44+
std::unique_ptr<BearSSL::WiFiClientSecure>client(new (std::nothrow) BearSSL::WiFiClientSecure);
45+
if (client == nullptr) {
46+
return;
47+
}
4548

4649
client->setFingerprint(fingerprint);
4750

libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ void loop() {
3838
// wait for WiFi connection
3939
if ((WiFiMulti.run() == WL_CONNECTED)) {
4040

41-
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
41+
std::unique_ptr<BearSSL::WiFiClientSecure> client(new (std::nothrow) BearSSL::WiFiClientSecure);
42+
if (client == nullptr) {
43+
return;
44+
}
4245

4346
bool mfln = client->probeMaxFragmentLength("tls.mbed.org", 443, 1024);
4447
Serial.printf("\nConnecting to https://tls.mbed.org\n");

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class TransportTraits
3737

3838
virtual std::unique_ptr<WiFiClient> create()
3939
{
40-
return std::unique_ptr<WiFiClient>(new WiFiClient());
40+
return std::unique_ptr<WiFiClient>(new (std::nothrow) WiFiClient());
4141
}
4242

4343
virtual bool verify(WiFiClient& client, const char* host)
@@ -59,8 +59,9 @@ class BearSSLTraits : public TransportTraits
5959

6060
std::unique_ptr<WiFiClient> create() override
6161
{
62-
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure();
63-
client->setFingerprint(_fingerprint);
62+
BearSSL::WiFiClientSecure *client = new (std::nothrow) BearSSL::WiFiClientSecure();
63+
if (client != nullptr)
64+
client->setFingerprint(_fingerprint);
6465
return std::unique_ptr<WiFiClient>(client);
6566
}
6667

@@ -182,7 +183,7 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
182183
if (!beginInternal(url, "https")) {
183184
return false;
184185
}
185-
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
186+
_transportTraits = TransportTraitsPtr(new (std::nothrow) BearSSLTraits(httpsFingerprint));
186187
if(!_transportTraits) {
187188
DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n");
188189
return false;
@@ -212,8 +213,8 @@ bool HTTPClient::begin(String url)
212213
if (!beginInternal(url, "http")) {
213214
return false;
214215
}
215-
_transportTraits = TransportTraitsPtr(new TransportTraits());
216-
return true;
216+
_transportTraits = TransportTraitsPtr(new (std::nothrow) TransportTraits());
217+
return _transportTraits != nullptr;
217218
}
218219

219220

@@ -290,9 +291,9 @@ bool HTTPClient::begin(String host, uint16_t port, String uri)
290291
_host = host;
291292
_port = port;
292293
_uri = uri;
293-
_transportTraits = TransportTraitsPtr(new TransportTraits());
294+
_transportTraits = TransportTraitsPtr(new (std::nothrow) TransportTraits());
294295
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d uri: %s\n", host.c_str(), port, uri.c_str());
295-
return true;
296+
return _transportTraits != nullptr;
296297
}
297298

298299

@@ -309,13 +310,13 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
309310
_port = port;
310311
_uri = uri;
311312

312-
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
313+
_transportTraits = TransportTraitsPtr(new (std::nothrow) BearSSLTraits(httpsFingerprint));
313314
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s BearSSL-httpsFingerprint:", host.c_str(), port, uri.c_str());
314315
for (size_t i=0; i < 20; i++) {
315316
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
316317
}
317318
DEBUG_HTTPCLIENT("\n");
318-
return true;
319+
return _transportTraits != nullptr;
319320
}
320321

321322

libraries/ESP8266HTTPUpdateServer/examples/SecureBearSSLUpdater/SecureBearSSLUpdater.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void setup()
106106

107107
MDNS.begin(host);
108108

109-
httpServer.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
109+
httpServer.getServer().setRSACert(new (std::nothrow) BearSSL::X509List(serverCert), new (std::nothrow) BearSSL::PrivateKey(serverKey));
110110
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
111111
httpServer.begin();
112112

libraries/ESP8266LLMNR/ESP8266LLMNR.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ bool LLMNRResponder::_restart() {
116116
if (igmp_joingroup(IP4_ADDR_ANY4, llmnr) != ERR_OK)
117117
return false;
118118

119-
_conn = new UdpContext;
119+
_conn = new (std::nothrow) UdpContext;
120+
if (!_conn)
121+
return false;
120122
_conn->ref();
121123

122124
if (!_conn->listen(IP_ADDR_ANY, LLMNR_PORT))

libraries/ESP8266SSDP/ESP8266SSDP.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ bool SSDPClass::begin() {
175175

176176
assert(NULL == _server);
177177

178-
_server = new UdpContext;
178+
_server = new (std::nothrow) UdpContext;
179+
if (_server == nullptr) {
180+
return false;
181+
}
179182
_server->ref();
180183

181184
IPAddress local = WiFi.localIP();
@@ -508,7 +511,9 @@ void SSDPClass::_onTimerStatic(SSDPClass* self) {
508511

509512
void SSDPClass::_startTimer() {
510513
_stopTimer();
511-
_timer = new SSDPTimer();
514+
_timer = new (std::nothrow) SSDPTimer();
515+
if (_timer == nullptr)
516+
return;
512517
ETSTimer* tm = &(_timer->timer);
513518
const int interval = 1000;
514519
os_timer_disarm(tm);

0 commit comments

Comments
 (0)