Ignore:
Timestamp:
May 20, 2013, 2:10:19 PM (12 years ago)
Author:
[email protected]
Message:

Make C API more robust against null contexts
https://bugs.webkit.org/show_bug.cgi?id=116462

Reviewed by Anders Carlsson.

Handle null contexts in a non-crashy way. It's a bug to ever call the
API with a null context, and the absence of a context means we can't
produce a meaningful result, so we still assert in debug builds.

Now where possible we detect and early return, returning null for any
pointer type, NaN for doubles, and false for any boolean result.

  • API/JSBase.cpp:

(JSEvaluateScript):
(JSCheckScriptSyntax):
(JSReportExtraMemoryCost):

  • API/JSContextRef.cpp:

(JSContextGetGlobalObject):
(JSContextGetGroup):
(JSContextGetGlobalContext):
(JSContextCreateBacktrace):

  • API/JSObjectRef.cpp:

(JSObjectMake):
(JSObjectMakeFunctionWithCallback):
(JSObjectMakeConstructor):
(JSObjectMakeFunction):
(JSObjectMakeArray):
(JSObjectMakeDate):
(JSObjectMakeError):
(JSObjectMakeRegExp):
(JSObjectGetPrototype):
(JSObjectSetPrototype):
(JSObjectHasProperty):
(JSObjectGetProperty):
(JSObjectSetProperty):
(JSObjectGetPropertyAtIndex):
(JSObjectSetPropertyAtIndex):
(JSObjectDeleteProperty):
(JSObjectCopyPropertyNames):

  • API/JSValueRef.cpp:

(JSValueGetType):
(JSValueIsUndefined):
(JSValueIsNull):
(JSValueIsBoolean):
(JSValueIsNumber):
(JSValueIsString):
(JSValueIsObject):
(JSValueIsObjectOfClass):
(JSValueIsEqual):
(JSValueIsStrictEqual):
(JSValueIsInstanceOfConstructor):
(JSValueMakeUndefined):
(JSValueMakeNull):
(JSValueMakeBoolean):
(JSValueMakeNumber):
(JSValueMakeString):
(JSValueMakeFromJSONString):
(JSValueCreateJSONString):
(JSValueToBoolean):
(JSValueToNumber):
(JSValueToStringCopy):
(JSValueToObject):
(JSValueProtect):

  • API/JSWeakObjectMapRefPrivate.cpp:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/API/JSValueRef.cpp

    r146494 r150381  
    6565::JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
    6666{
     67    if (!ctx) {
     68        ASSERT_NOT_REACHED();
     69        return kJSTypeUndefined;
     70    }
    6771    ExecState* exec = toJS(ctx);
    6872    APIEntryShim entryShim(exec);
     
    8690bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
    8791{
     92    if (!ctx) {
     93        ASSERT_NOT_REACHED();
     94        return false;
     95    }
    8896    ExecState* exec = toJS(ctx);
    8997    APIEntryShim entryShim(exec);
     
    95103bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
    96104{
     105    if (!ctx) {
     106        ASSERT_NOT_REACHED();
     107        return false;
     108    }
    97109    ExecState* exec = toJS(ctx);
    98110    APIEntryShim entryShim(exec);
     
    104116bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
    105117{
     118    if (!ctx) {
     119        ASSERT_NOT_REACHED();
     120        return false;
     121    }
    106122    ExecState* exec = toJS(ctx);
    107123    APIEntryShim entryShim(exec);
     
    113129bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
    114130{
     131    if (!ctx) {
     132        ASSERT_NOT_REACHED();
     133        return false;
     134    }
    115135    ExecState* exec = toJS(ctx);
    116136    APIEntryShim entryShim(exec);
     
    122142bool JSValueIsString(JSContextRef ctx, JSValueRef value)
    123143{
     144    if (!ctx) {
     145        ASSERT_NOT_REACHED();
     146        return false;
     147    }
    124148    ExecState* exec = toJS(ctx);
    125149    APIEntryShim entryShim(exec);
     
    131155bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
    132156{
     157    if (!ctx) {
     158        ASSERT_NOT_REACHED();
     159        return false;
     160    }
    133161    ExecState* exec = toJS(ctx);
    134162    APIEntryShim entryShim(exec);
     
    140168bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
    141169{
     170    if (!ctx || !jsClass) {
     171        ASSERT_NOT_REACHED();
     172        return false;
     173    }
    142174    ExecState* exec = toJS(ctx);
    143175    APIEntryShim entryShim(exec);
     
    160192bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
    161193{
     194    if (!ctx) {
     195        ASSERT_NOT_REACHED();
     196        return false;
     197    }
    162198    ExecState* exec = toJS(ctx);
    163199    APIEntryShim entryShim(exec);
     
    177213bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
    178214{
     215    if (!ctx) {
     216        ASSERT_NOT_REACHED();
     217        return false;
     218    }
    179219    ExecState* exec = toJS(ctx);
    180220    APIEntryShim entryShim(exec);
     
    188228bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
    189229{
     230    if (!ctx) {
     231        ASSERT_NOT_REACHED();
     232        return false;
     233    }
    190234    ExecState* exec = toJS(ctx);
    191235    APIEntryShim entryShim(exec);
     
    207251JSValueRef JSValueMakeUndefined(JSContextRef ctx)
    208252{
     253    if (!ctx) {
     254        ASSERT_NOT_REACHED();
     255        return 0;
     256    }
    209257    ExecState* exec = toJS(ctx);
    210258    APIEntryShim entryShim(exec);
     
    215263JSValueRef JSValueMakeNull(JSContextRef ctx)
    216264{
     265    if (!ctx) {
     266        ASSERT_NOT_REACHED();
     267        return 0;
     268    }
    217269    ExecState* exec = toJS(ctx);
    218270    APIEntryShim entryShim(exec);
     
    223275JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value)
    224276{
     277    if (!ctx) {
     278        ASSERT_NOT_REACHED();
     279        return 0;
     280    }
    225281    ExecState* exec = toJS(ctx);
    226282    APIEntryShim entryShim(exec);
     
    231287JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
    232288{
     289    if (!ctx) {
     290        ASSERT_NOT_REACHED();
     291        return 0;
     292    }
    233293    ExecState* exec = toJS(ctx);
    234294    APIEntryShim entryShim(exec);
     
    245305JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
    246306{
     307    if (!ctx) {
     308        ASSERT_NOT_REACHED();
     309        return 0;
     310    }
    247311    ExecState* exec = toJS(ctx);
    248312    APIEntryShim entryShim(exec);
     
    253317JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string)
    254318{
     319    if (!ctx) {
     320        ASSERT_NOT_REACHED();
     321        return 0;
     322    }
    255323    ExecState* exec = toJS(ctx);
    256324    APIEntryShim entryShim(exec);
     
    267335JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception)
    268336{
     337    if (!ctx) {
     338        ASSERT_NOT_REACHED();
     339        return 0;
     340    }
    269341    ExecState* exec = toJS(ctx);
    270342    APIEntryShim entryShim(exec);
     
    284356bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
    285357{
     358    if (!ctx) {
     359        ASSERT_NOT_REACHED();
     360        return false;
     361    }
    286362    ExecState* exec = toJS(ctx);
    287363    APIEntryShim entryShim(exec);
     
    293369double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
    294370{
     371    if (!ctx) {
     372        ASSERT_NOT_REACHED();
     373        return QNaN;
     374    }
    295375    ExecState* exec = toJS(ctx);
    296376    APIEntryShim entryShim(exec);
     
    310390JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
    311391{
     392    if (!ctx) {
     393        ASSERT_NOT_REACHED();
     394        return 0;
     395    }
    312396    ExecState* exec = toJS(ctx);
    313397    APIEntryShim entryShim(exec);
     
    327411JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
    328412{
     413    if (!ctx) {
     414        ASSERT_NOT_REACHED();
     415        return 0;
     416    }
    329417    ExecState* exec = toJS(ctx);
    330418    APIEntryShim entryShim(exec);
     
    344432void JSValueProtect(JSContextRef ctx, JSValueRef value)
    345433{
     434    if (!ctx) {
     435        ASSERT_NOT_REACHED();
     436        return;
     437    }
    346438    ExecState* exec = toJS(ctx);
    347439    APIEntryShim entryShim(exec);
Note: See TracChangeset for help on using the changeset viewer.