Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 100590a

Browse files
committed
libfoundation: Better assertions for null buffer pointers.
* When a function accepts a pointer + size, allow the pointer to be null iff the size is 0. * Fast-path empty results when a ValueRef is passed a buffer of length 0.
1 parent a1eb9d3 commit 100590a

2 files changed

Lines changed: 38 additions & 16 deletions

File tree

libfoundation/src/foundation-stream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ bool MCStreamGetAvailableForRead(MCStreamRef self, size_t& r_available)
274274
bool MCStreamRead(MCStreamRef self, void *p_buffer, size_t p_amount)
275275
{
276276
__MCAssertIsStream(self);
277-
MCAssert(nil != p_buffer);
277+
MCAssert(nil != p_buffer || 0 == p_amount);
278278

279279
if (self -> callbacks -> read == nil)
280280
return false;
@@ -293,7 +293,7 @@ bool MCStreamGetAvailableForWrite(MCStreamRef self, size_t& r_available)
293293
bool MCStreamWrite(MCStreamRef self, const void *p_buffer, size_t p_amount)
294294
{
295295
__MCAssertIsStream(self);
296-
MCAssert(nil != p_buffer);
296+
MCAssert(nil != p_buffer || 0 == p_amount);
297297

298298
if (self -> callbacks -> write == nil)
299299
return false;

libfoundation/src/foundation-string.cpp

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,19 @@ bool MCStringIsEqualToCString(MCStringRef p_string, const char *p_cstring, MCStr
287287
// the specified encoding.
288288
bool MCStringCreateWithBytes(const byte_t *p_bytes, uindex_t p_byte_count, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string)
289289
{
290-
MCAssert(nil != p_bytes);
290+
if (0 == p_byte_count)
291+
{
292+
if (nil != kMCEmptyString)
293+
{
294+
r_string = MCValueRetain(kMCEmptyString);
295+
return true;
296+
}
297+
}
298+
else
299+
{
300+
MCAssert(nil != p_bytes);
301+
}
302+
291303
MCAssert(!p_is_external_rep);
292304

293305
switch (p_encoding)
@@ -399,14 +411,19 @@ bool MCStringCreateWithBytes(const byte_t *p_bytes, uindex_t p_byte_count, MCStr
399411

400412
bool MCStringCreateWithBytesAndRelease(byte_t *p_bytes, uindex_t p_byte_count, MCStringEncoding p_encoding, bool p_is_external_rep, MCStringRef& r_string)
401413
{
402-
if ((p_bytes == nil || p_byte_count == 0) && kMCEmptyString != nil)
403-
{
404-
r_string = MCValueRetain(kMCEmptyString);
405-
free(p_bytes);
406-
return true;
407-
}
408-
409-
MCAssert(nil != p_bytes);
414+
if (p_byte_count == 0)
415+
{
416+
if (kMCEmptyString != nil)
417+
{
418+
r_string = MCValueRetain(kMCEmptyString);
419+
free(p_bytes);
420+
return true;
421+
}
422+
}
423+
else
424+
{
425+
MCAssert(nil != p_bytes);
426+
}
410427

411428
MCStringRef t_string;
412429
t_string = nil;
@@ -451,12 +468,17 @@ bool MCStringCreateWithBytesAndRelease(byte_t *p_bytes, uindex_t p_byte_count, M
451468

452469
bool MCStringCreateWithChars(const unichar_t *p_chars, uindex_t p_char_count, MCStringRef& r_string)
453470
{
454-
MCAssert(nil != p_chars);
455-
456-
if (p_char_count == 0 && kMCEmptyString != nil)
471+
if (p_char_count == 0)
457472
{
458-
r_string = MCValueRetain(kMCEmptyString);
459-
return true;
473+
if (kMCEmptyString != nil)
474+
{
475+
r_string = MCValueRetain(kMCEmptyString);
476+
return true;
477+
}
478+
}
479+
else
480+
{
481+
MCAssert(nil != p_chars);
460482
}
461483

462484
bool t_success;

0 commit comments

Comments
 (0)