Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungBreeze TTS 2 vs ElevenLabs: Open Source TTS Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungAgentic AI vs Generative AI: The 2026 Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungI made my agent prove every quote against the source document(23.09.2026 um 05:45 Uhr)
Sichere Programmierung8mb.video Alternative: Skip the Line, Skip the Upsell(23.09.2026 um 05:47 Uhr)
Sichere ProgrammierungBuilding a GTA 6 JSON API for entities and current status(23.09.2026 um 05:52 Uhr)
Sichere ProgrammierungEvery filter needs a documented exception(23.09.2026 um 06:01 Uhr)
Sichere ProgrammierungBreeze TTS 2 vs ElevenLabs: Open Source TTS Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungAgentic AI vs Generative AI: The 2026 Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungI made my agent prove every quote against the source document(23.09.2026 um 05:45 Uhr)
Sichere Programmierung8mb.video Alternative: Skip the Line, Skip the Upsell(23.09.2026 um 05:47 Uhr)
Sichere ProgrammierungBuilding a GTA 6 JSON API for entities and current status(23.09.2026 um 05:52 Uhr)
Sichere ProgrammierungEvery filter needs a documented exception(23.09.2026 um 06:01 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

TIL emalloc() auto-exits on out-of-memory errors

I was tired of writing this over and over: double* data = (double*)malloc(20 * sizeof(double)); if (data == NULL) { fputs("out of memory", stderr); abort(); } And today I learned that there's a family of e___() functions like…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

I was tired of writing this over and over:




double* data = (double*)malloc(20 * sizeof(double));
if (data == NULL) {
fputs("out of memory", stderr);
abort();
}






And today I learned that there's a family of e___() functions like emalloc() and ecalloc() which will exit the process if the malloc()-returned pointer is NULL. These functions are only present in the BSD family of operating system's util.h system header. They aren't standard C functions.




#include <util.h>

void (*)(int, const char *, ...) esetfunc(void (*)(int, const char *, ...));

int easprintf(char ** restrict str, const char * restrict fmt, ...);

FILE * efopen(const char *p, const char *m);

void * ecalloc(size_t n, size_t s);

void * emalloc(size_t n);

void * erealloc(void *p, size_t n);

void ereallocarr(void *p, size_t n, size_t s);

char * estrdup(const char *s);

char * estrndup(const char *s, size_t len);

size_t estrlcat(char *dst, const char *src, size_t len);

size_t estrlcpy(char *dst, const char *src, size_t len);

intmax_t estrtoi(const char * nptr, int base, intmax_t lo, intmax_t hi);

uintmax_t estrtou(const char * nptr, int base, uintmax_t lo, uintmax_t hi);

int evasprintf(char ** restrict str, const char * restrict fmt, ...);







The easprintf(), efopen(), ecalloc(), emalloc(), erealloc(), ereallocarr(), estrdup(), estrndup(), estrlcat(), estrlcpy(), estrtoi(), estrtou(), and evasprintf() functions operate exactly as the corresponding functions that do not start with an e except that in case of an error, they call the installed error handler that can be configured with esetfunc().



For the string handling functions, it is an error when the destination buffer is not large enough to hold the complete string. For functions that allocate memory or open a file, it is an error when they would return a null pointer. The default error handler is err(). The function esetfunc() returns the previous error handler function. A NULL error handler will just call exit().




emalloc(3) - NetBSD Manual Pages



These functions are relatively simple. You can easily implement them yourself.




// I don't know if static or inline is better.
inline void * emalloc(size_t n) {
void *p = malloc(n);
if (p == NULL) {
fputs("out of memory", stderr);
abort();
}
return p;
}

// Do the same wrapper thing for all the other functions.






I just thought it was cool that this was a thing that is built into an operating system's C standard library. That's batteries included for sure.






What do other languages do on out-of-memory errors?




  • Java: Throw an exception

  • C++: Throw an exception

  • Rust: Abort or panic

  • JavaScript: Crash the engine

  • Python: Throws a special exception

  • Go: Terminate



And then there's C: Return a null pointer. 🤷‍♀️ Great if you need that control, a hassle if you want the happy path.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten TIL emalloc() auto-exits on out-of-memory errors

Thematisch verwandte Begriffe: emalloc, autoexits, outofmemory, errors · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-17636 | IBM Financial Transaction Manager (FTM) for RedHat OpenShift could allow…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick