lvsti / CEF.swift

Swift bindings for the Chromium Embedded Framework
BSD 3-Clause "New" or "Revised" License
94 stars 29 forks source link

Exception occur when getting stringValue from an empty-string CEFV8Value #29

Closed WarWithinMe closed 6 years ago

WarWithinMe commented 6 years ago

Branch: 3239

How to reproduce:

let val = CEFV8Value.createString("")
print(val.stringValue)

Then a exception Chrome_InProcRendererThread (34): EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) is throw.

Currently there's no way to examine if the val is empty string or not, so I don't know how to avoid this.

lvsti commented 6 years ago

The root of the problem is this:

// include/internal/cef_string_wrappers.h 

  ///
  // Returns true if the string is empty.
  ///
  bool empty() const { return (string_ == NULL || string_->length == 0); }

  ///
  // Create a userfree structure and give it ownership of this class' string
  // data. This class will be disassociated from the data. May return NULL if
  // this string class currently contains no data.
  ///
  userfree_struct_type DetachToUserFree() {
    if (empty())
      return NULL;
  ...

So in APIs which create and return a string (userfree), CEF doesn't distinguish between null and zero-length instances, and eventually erases this information by simply returning null for both cases. This is a problem in CEFV8Value.stringValue (and many other CEFswift APIs) since I've carelessly been force-unwrapping these returned pointers.

The quick fix will be to return an empty string when a null pointer is found. However, there will be no way to determine if the value indeed was an empty string, or some other type and you are just seeing an error case fallback.

lvsti commented 6 years ago

I pushed the fix to the 3282 branch, let me know if it helped.

WarWithinMe commented 6 years ago

Works like a charm. Thank you.