<div dir="ltr">Also, it would be nice (but again this isn't immediately affecting me so I can't really put too much pressure here) if it continued to return None in case of error.  Any code that would be affected by changing the error return value from None to "" was very likely broken already and this just exposed an existing bug.</div><br><div class="gmail_quote"><div dir="ltr">On Wed, Nov 15, 2017 at 7:52 PM Jason Molenda <<a href="mailto:jmolenda@apple.com">jmolenda@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Yeah, I'm going to check in one based on my little example program.  The only tricky bit is<br>
<br>
<br>
<br>
> >     const char *invalid_memory_string = (char*)0x100; // lower 4k is always PAGEZERO & unreadable on darwin<br>
<br>
<br>
I'll need some address that is unmapped for other platforms.  Maybe address -1 or something.<br>
<br>
<br>
<br>
<br>
> On Nov 15, 2017, at 7:48 PM, Zachary Turner <<a href="mailto:zturner@google.com" target="_blank">zturner@google.com</a>> wrote:<br>
><br>
> Probably obvious, can you add some tests to verify the new behavior?<br>
><br>
> On Wed, Nov 15, 2017 at 7:47 PM Zachary Turner <<a href="mailto:zturner@google.com" target="_blank">zturner@google.com</a>> wrote:<br>
> Yea, if there is a valid string there, it should definitely be returning "", hard to argue with that.<br>
><br>
> On Wed, Nov 15, 2017 at 7:06 PM Jason Molenda <<a href="mailto:jmolenda@apple.com" target="_blank">jmolenda@apple.com</a>> wrote:<br>
> The general point you're making is reasonable, and something like Thread::GetStopDescription is not clear which the correct behavior should be.<br>
><br>
> But I think we can all agree that Process::ReadCStringFromMemory() returning a None object when there is an empty c-string is incorrect.  People are writing code calling Process::ReadCStringFromMemory(), checking the SBError object if it was successful, and then treating the return value as if it were a string, which is reasonable to do.<br>
><br>
> I'll check in the fix tomorrow, and update the TestMiniDumpNew.py test, thanks.<br>
><br>
> J<br>
><br>
> > On Nov 15, 2017, at 6:56 PM, Zachary Turner <<a href="mailto:zturner@google.com" target="_blank">zturner@google.com</a>> wrote:<br>
> ><br>
> > I don't really feel strongly about how you fix it.  I'm sure there was a good reason for making it do that, but at this point I don't remember what it is and I don't think undoing it will cause a problem.<br>
> ><br>
> > That said, part of the difficulty of having an API such as this is that Hyrum's Law [<a href="http://www.hyrumslaw.com/" rel="noreferrer" target="_blank">http://www.hyrumslaw.com/</a>] is going to continue to bite you.  The reason this broke is because there was no test guaranteeing that this behavior that people were relying on did not change.<br>
> ><br>
> > As a general rule, it's impossible to guarantee that no observable behavior of an API will ever change, so unless there was a test for the original behavior (which is documentation that this is behavior people are allowed to rely on), I don't really consider it broken.<br>
> ><br>
> > Even if we are in the business of guaranteeing that the API itself won't change, we really can't be in the business of guaranteeing that no observable behavior of the API will ever change.  That's going to be an endless maintenance nightmare.<br>
> ><br>
> > On Wed, Nov 15, 2017 at 6:47 PM Jason Molenda <<a href="mailto:jmolenda@apple.com" target="_blank">jmolenda@apple.com</a>> wrote:<br>
> > Hi Zachary, reviving a problem I initially found a year ago -- in r253487 where a swig typemap was changed for string reading methods.<br>
> ><br>
> > The problem we're seeing with this change is that SBProcess::ReadCStringFromMemory() now returns a None object when you try to read a zero-length string, and this is breaking a couple of groups' python scripts here at Apple.  (it was a low priority thing for me to fix, until some behavior changed recently and it started biting the groups a lot more frequently).<br>
> ><br>
> > SBProcess::ReadCStringFromMemory() takes an SBError object and returns a string.  We have three cases:<br>
> ><br>
> ><br>
> > 1 Non-zero length string read.  SBError says it was successful, string should be returned.<br>
> ><br>
> > 2 Zero-length / empty string read.  SBError says it was successful, string should be returned (Python None object is returned right now)<br>
> ><br>
> > 3 Memory read error.  SBError says it is an error, ideally None object should be returned.<br>
> ><br>
> ><br>
> > For instance,<br>
> ><br>
> > #include <stdlib.h><br>
> > int main ()<br>
> > {<br>
> >     const char *empty_string = "";<br>
> >     const char *one_letter_string = "1";<br>
> >     const char *invalid_memory_string = (char*)0x100; // lower 4k is always PAGEZERO & unreadable on darwin<br>
> >     return empty_string[0] + one_letter_string[0]; // breakpoint here<br>
> > }<br>
> ><br>
> > we'll see:<br>
> ><br>
> >   (lldb) br s -p breakpoint<br>
> >   (lldb) r<br>
> >   (lldb) p empty_string<br>
> >   (const char *) $0 = 0x0000000100000fae ""<br>
> >   (lldb) p one_letter_string<br>
> >   (const char *) $1 = 0x0000000100000faf "1"<br>
> >   (lldb) p invalid_memory_string<br>
> >   (const char *) $2 = 0x0000000000000100 ""<br>
> >   (lldb) scri<br>
> >   >>> err = lldb.SBError()<br>
> ><br>
> >   >>> print lldb.process.ReadCStringFromMemory(0x0000000100000fae, 2048, err)<br>
> >   None<br>
> >   >>> print err<br>
> >   success<br>
> ><br>
> >   >>> print lldb.process.ReadCStringFromMemory(0x0000000100000faf, 2048, err)<br>
> >   1<br>
> >   >>> print err<br>
> >   success<br>
> ><br>
> >   >>> print lldb.process.ReadCStringFromMemory(0x0000000000000100, 2048, err)<br>
> >   None<br>
> >   >>> print err<br>
> >   error: memory read failed for 0x0<br>
> >   >>> print err.Success()<br>
> >   False<br>
> >   >>><br>
> ><br>
> ><br>
> > Before r253487, the read of a zero-length string and the read of an invalid address would both return a zero length python string (and the latter would set the SBError).  After the change, both of these return a python None object (and the latter sets the SBError).<br>
> ><br>
> ><br>
> > I haven't worked with the typemaps before -- I can restore the previous behavior where an empty Python string is returned for both the zero-length string and for the unreadable address.  I don't see how I can access the SBError object used earlier in these methods.<br>
> ><br>
> ><br>
> > diff --git i/scripts/Python/python-typemaps.swig w/scripts/Python/python-typemaps.swig<br>
> > index df16a6d27b3..29e5d9b156d 100644<br>
> > --- i/scripts/Python/python-typemaps.swig<br>
> > +++ w/scripts/Python/python-typemaps.swig<br>
> > @@ -102,7 +102,8 @@<br>
> >  %typemap(argout) (char *dst, size_t dst_len) {<br>
> >     Py_XDECREF($result);   /* Blow away any previous result */<br>
> >     if (result == 0) {<br>
> > -      $result = Py_None;<br>
> > +      lldb_private::PythonString string("");<br>
> > +      $result = string.release();<br>
> >        Py_INCREF($result);<br>
> >     } else {<br>
> >        llvm::StringRef ref(static_cast<const char*>($1), result);<br>
> ><br>
> ><br>
> ><br>
> > This does cause one test in the testsuite to fail --<br>
> ><br>
> > ======================================================================<br>
> > FAIL: test_snapshot_minidump (TestMiniDumpNew.MiniDumpNewTestCase)<br>
> >    Test that if we load a snapshot minidump file (meaning the process<br>
> > ----------------------------------------------------------------------<br>
> > Traceback (most recent call last):<br>
> >   File "/Volumes/newwork/github/stable/lldb/packages/Python/lldbsuite/test/functionalities/postmortem/minidump-new/TestMiniDumpNew.py", line 88, in test_snapshot_minidump<br>
> >     self.assertEqual(stop_description, None)<br>
> > AssertionError: '' != None<br>
> > Config=x86_64-/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang<br>
> > ----------------------------------------------------------------------<br>
> ><br>
> ><br>
> > which is doing<br>
> ><br>
> ><br>
> >         thread = self.process.GetThreadAtIndex(0)<br>
> >         self.assertEqual(thread.GetStopReason(), lldb.eStopReasonNone)<br>
> >         stop_description = thread.GetStopDescription(256)<br>
> >         self.assertEqual(stop_description, None)<br>
> ><br>
> ><br>
> > SBThread::GetStopDescription doesn't have an SBError object to indicate that there is no stop description for eStopReasonNone.  I don't think this will be a problem if eStopReasonNone is returning an empty python string for the StopDescription.<br>
> ><br>
> ><br>
> > I'm not wedded to my current patch, but we do have to come up with something that can return a zero length python string for a method like SBProcess::ReadCStringFromMemory.<br>
><br>
<br>
</blockquote></div>