Ignore:
Timestamp:
Aug 11, 2009, 11:22:41 PM (16 years ago)
Author:
[email protected]
Message:

Make it harder to misuse try* allocation routines
https://bugs.webkit.org/show_bug.cgi?id=27469

Reviewed by Gavin Barraclough

Jump through a few hoops to make it much harder to accidentally
miss null-checking of values returned by the try-* allocation
routines.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/UString.cpp

    r46180 r47092  
    6969static inline size_t maxUChars() { return std::numeric_limits<size_t>::max() / sizeof(UChar); }
    7070
    71 static inline UChar* allocChars(size_t length)
     71static inline PossiblyNull<UChar*> allocChars(size_t length)
    7272{
    7373    ASSERT(length);
    7474    if (length > maxUChars())
    7575        return 0;
    76     return static_cast<UChar*>(tryFastMalloc(sizeof(UChar) * length));
    77 }
    78 
    79 static inline UChar* reallocChars(UChar* buffer, size_t length)
     76    return tryFastMalloc(sizeof(UChar) * length);
     77}
     78
     79static inline PossiblyNull<UChar*> reallocChars(UChar* buffer, size_t length)
    8080{
    8181    ASSERT(length);
    8282    if (length > maxUChars())
    8383        return 0;
    84     return static_cast<UChar*>(tryFastRealloc(buffer, sizeof(UChar) * length));
     84    return tryFastRealloc(buffer, sizeof(UChar) * length);
    8585}
    8686
     
    481481        size_t newCapacity = expandedSize(requiredLength, base->preCapacity);
    482482        UChar* oldBuf = base->buf;
    483         base->buf = reallocChars(base->buf, newCapacity);
    484         if (!base->buf) {
     483        if (!reallocChars(base->buf, newCapacity).getValue(base->buf)) {
    485484            base->buf = oldBuf;
    486485            return false;
     
    513512    size_t newCapacity = expandedSize(capacity, base->preCapacity);
    514513    UChar* oldBuf = base->buf;
    515     base->buf = reallocChars(base->buf, newCapacity);
    516     if (!base->buf) {
     514    if (!reallocChars(base->buf, newCapacity).getValue(base->buf)) {
    517515        base->buf = oldBuf;
    518516        return false;
     
    541539        int delta = newCapacity - base->capacity - base->preCapacity;
    542540
    543         UChar* newBuf = allocChars(newCapacity);
    544         if (!newBuf) {
     541        UChar* newBuf;
     542        if (!allocChars(newCapacity).getValue(newBuf)) {
    545543            makeNull();
    546544            return;
     
    567565
    568566    size_t length = strlen(c);
    569     UChar* d = allocChars(length);
    570     if (!d)
     567    UChar* d;
     568    if (!allocChars(length).getValue(d))
    571569        return &UString::Rep::null();
    572570    else {
     
    657655        // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
    658656        size_t newCapacity = expandedSize(length, 0);
    659         UChar* d = allocChars(newCapacity);
    660         if (!d)
     657        UChar* d;
     658        if (!allocChars(newCapacity).getValue(d))
    661659            rep = &UString::Rep::null();
    662660        else {
     
    713711        // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
    714712        size_t newCapacity = expandedSize(length, 0);
    715         UChar* d = allocChars(newCapacity);
    716         if (!d)
     713        UChar* d;
     714        if (!allocChars(newCapacity).getValue(d))
    717715            rep = &UString::Rep::null();
    718716        else {
     
    801799    // a does not qualify for append, and b does not qualify for prepend, gotta make a whole new string
    802800    size_t newCapacity = expandedSize(length, 0);
    803     UChar* d = allocChars(newCapacity);
    804     if (!d)
     801    UChar* d;
     802    if (!allocChars(newCapacity).getValue(d))
    805803        return 0;
    806804    copyChars(d, a->data(), aSize);
     
    10771075        return "";
    10781076
    1079     UChar* buffer = allocChars(totalLength);
    1080     if (!buffer)
     1077    UChar* buffer;
     1078    if (!allocChars(totalLength).getValue(buffer))
    10811079        return null();
    10821080
     
    11061104        return "";
    11071105
    1108     UChar* buffer = allocChars(totalLength);
    1109     if (!buffer)
     1106    UChar* buffer;
     1107    if (!allocChars(totalLength).getValue(buffer))
    11101108        return null();
    11111109
     
    11541152        // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
    11551153        size_t newCapacity = expandedSize(length, 0);
    1156         UChar* d = allocChars(newCapacity);
    1157         if (!d)
     1154        UChar* d;
     1155        if (!allocChars(newCapacity).getValue(d))
    11581156            makeNull();
    11591157        else {
     
    12071205        // this is empty - must make a new m_rep because we don't want to pollute the shared empty one
    12081206        size_t newCapacity = expandedSize(1, 0);
    1209         UChar* d = allocChars(newCapacity);
    1210         if (!d)
     1207        UChar* d;
     1208        if (!allocChars(newCapacity).getValue(d))
    12111209            makeNull();
    12121210        else {
     
    12351233        // This is shared in some way that prevents us from modifying base, so we must make a whole new string.
    12361234        size_t newCapacity = expandedSize(length + 1, 0);
    1237         UChar* d = allocChars(newCapacity);
    1238         if (!d)
     1235        UChar* d;
     1236        if (!allocChars(newCapacity).getValue(d))
    12391237            makeNull();
    12401238        else {
     
    13141312        m_rep->len = l;
    13151313    } else {
    1316         d = allocChars(l);
    1317         if (!d) {
     1314        if (!allocChars(l).getValue(d)) {
    13181315            makeNull();
    13191316            return *this;
Note: See TracChangeset for help on using the changeset viewer.