8000 esp8266/posix_helpers: Set ENOMEM on memory alloc failure. · rlucia/micropython@9b4666d · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b4666d

Browse files
committed
esp8266/posix_helpers: Set ENOMEM on memory alloc failure.
POSIX requires malloc(), etc. to set ENOMEM on the failure, and e.g. BerkeleyDB relies on this: http://pubs.opengroup.org/onlinepubs/9699919799/functions/malloc.html This should fix confusing OSError exceptions with 0 error code when working with btree module.
1 parent 5671a11 commit 9b4666d

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

ports/esp8266/posix_helpers.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,33 @@
2626

2727
#include <stdint.h>
2828
#include <stdio.h>
29+
#include <errno.h>
2930
#include "py/mphal.h"
3031
#include "py/gc.h"
3132

3233
// Functions for external libs like axTLS, BerkeleyDB, etc.
3334

3435
void *malloc(size_t size) {
35-
return gc_alloc(size, false);
36+
void *p = gc_alloc(size, false);
37+
if (p == NULL) {
38+
// POSIX requires ENOMEM to be set in case of error
39+
errno = ENOMEM;
40+
}
41+
return p;
3642
}
3743
void free(void *ptr) {
3844
gc_free(ptr);
3945
}
4046
void *calloc(size_t nmemb, size_t size) {
41-
return m_malloc0(nmemb * size);
47+
return malloc(nmemb * size);
4248
}
4349
void *realloc(void *ptr, size_t size) {
44-
return gc_realloc(ptr, size, true);
50+
void *p = gc_realloc(ptr, size, true);
51+
if (p == NULL) {
52+
// POSIX requires ENOMEM to be set in case of error
53+
errno = ENOMEM;
54+
}
55+
return p;
4556
}
4657

4758
#define PLATFORM_HTONL(_n) ((uint32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) ))

0 commit comments

Comments
 (0)
0