From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Philipp Stephani
>=C2=A0
> > > +static Lisp_Object
> > > +internal_catch_all_1 (Lisp_Object (*function) (void *), voi= d *argument)
> >
> > Can you tell why you needed this (and the similar internal_catch_= all)?
> > Is that only because the callbacks could signal an error, or is t= here
> > another reason?=C2=A0 If the former, I'd prefer to simplify t= he code and
> > its maintenance by treating the error condition in a less drastic=
> > manner, and avoiding the call to xsignal.
>
> The callbacks (especially insert and before-/after-change-hook) can ex= it
> nonlocally, but these nonlocal exits may not escape the Jansson callba= ck.
> Therefore all nonlocal exits must be caught here.
Why can't you use record_unwind_protect, as we normally do in these
situations?
> > And btw, how can size be greater than SIZE_MAX in this case?=C2= =A0 This is
> > a valid Lisp object, isn't it?=C2=A0 (There are more such tes= ts in the
> > patch, e.g. in lisp_to_json, and I think they, too, are redundant= .)
>
> Depends on the range of ptrdiff_t and size_t. IIUC nothing in the C
> standard guarantees PTRDIFF_MAX <=3D SIZE_MAX.
I wasn't talking about PTRDIFF_MAX, I was talking about 'size',= which
is the number of bytes in a Lisp string.=C2=A0 Since that Lisp string is a<= br> valid Lisp object, how can its size be greater than SIZE_MAX?=C2=A0 I don= 39;t
think there's a way of creating such a Lisp string in Emacs, because
functions that allocate memory for strings will prevent that.Then I think we should at least add an assertion to = document this.=C2=A0
> > > +=C2=A0 =C2=A0 =C2=A0 *json =3D json_check (json_array ());<= br> > > > +=C2=A0 =C2=A0 =C2=A0 ptrdiff_t count =3D SPECPDL_INDEX ();<= br> > > > +=C2=A0 =C2=A0 =C2=A0 record_unwind_protect_ptr (json_releas= e_object, json);
> > > +=C2=A0 =C2=A0 =C2=A0 for (ptrdiff_t i =3D 0; i < size; += +i)
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 {
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 int status
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =3D json_array_ap= pend_new (*json, lisp_to_json (AREF (lisp,
> > i)));
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (status =3D=3D -1)
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 json_out_of_memor= y ();
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 eassert (status =3D=3D 0= );
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 }
> > > +=C2=A0 =C2=A0 =C2=A0 eassert (json_array_size (*json) =3D= =3D size);
> > > +=C2=A0 =C2=A0 =C2=A0 clear_unwind_protect (count);
> > > +=C2=A0 =C2=A0 =C2=A0 return unbind_to (count, Qnil);
> >
> > This, too, sounds more complex than it should: you record
> > unwind-protect just so lisp_to_json's subroutines could signa= l an
> > error due to insufficient memory, right?=C2=A0 Why can't we h= ave the
> > out-of-memory check only inside this loop, which you already do, = and
> > avoid the checks on lower levels (which undoubtedly cost us extra=
> > cycles)?=C2=A0 What do those extra checks in json_check buy us? t= he errors
> > they signal are no more informative than the one in the loop, AFA= ICT.
>
> I don't understand what you mean. We need to check the return valu= es of all
> functions if we want to to use them later.
Yes, but what problems can cause these return value to be invalid?
AFAICT, only out-of-memory conditions, and that can be checked only
once, there's no need to check every single allocation, because once
an allocation fails, all the rest will too.=But if the first succeeds, the second can still fail, so we do need to= check all of them.=C2=A0
> > > +static Lisp_Object
> > > +json_insert (void *data)
> > > +{
> > > +=C2=A0 const struct json_buffer_and_size *buffer_and_size = =3D data;
> > > +=C2=A0 if (buffer_and_size->size > PTRDIFF_MAX)
> > > +=C2=A0 =C2=A0 xsignal1 (Qoverflow_error, build_string (&quo= t;buffer too large"));
> > > +=C2=A0 insert (buffer_and_size->buffer, buffer_and_size-= >size);
> >
> > I don't think we need this test here, as 'insert' alr= eady has the
> > equivalent test in one of its subroutines.
>
> It can't, because it takes the byte length as ptrdiff_t. We need t= o check
> before whether the size is actually in the valid range of ptrdiff_t.
I'm sorry, but I don't see why we should support such exotic
situations only for this one feature.=C2=A0 In all other cases we use
either ptrdiff_t type or EMACS_INT type, and these issues disappear
then.=C2=A0 Trying to support the SIZE_MAX > PTRDIFF_MAX situation cause= s
the code to be much more complicated, harder to maintain, and more
expensive at run time than it should be.=C2=A0<= div>We can't avoid these checks. The API returns size_t, so we can only= assume that the numbers are in the valid range of size_t, which is larger = than the ones for positive ptrdiff_t's. There's no way around this.=
I'm not even sur= e there are
such platforms out there that Emacs supports,
but if there are, we
already have a gazillion problems like that all over our code.=C2=A0Just because other parts of the codebase are bugg= y doesn't mean we need to introduce more bugs in new code.= =C2=A0I
object to having such code just for this reason, sorry. We can't avoid it.=C2=A0
> > > +=C2=A0 =C2=A0 case JSON_INTEGER:
> > > +=C2=A0 =C2=A0 =C2=A0 {
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 json_int_t value =3D json_integ= er_value (json);
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (FIXNUM_OVERFLOW_P (value))<= br> > > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xsignal1 (Qoverflow_erro= r,
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 build_string ("JSON integer is too large"));
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return make_number (value);
> >
> > This overflow test is also redundant, as make_number already does= it.
>
> It can't, because json_int_t can be larger than EMACS_INT.
OK, but then I think we should consider returning a float value, or a
cons of 2 integers.=C2=A0 If these situations are frequent enough, users
will thank us, and if they are very infrequent, they will never see
such values, and we gain code simplicity and less non-local exits.Returning a float (using make_natnum_or_float) = might work, but in the end I've decided against it because it could sil= ently drop precision. I think that's worse than signaling an error.=C2=A0<= div>
> > > +=C2=A0 =C2=A0 case JSON_STRING:
> > > +=C2=A0 =C2=A0 =C2=A0 {
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 size_t size =3D json_string_len= gth (json);
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (FIXNUM_OVERFLOW_P (size)) > > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xsignal1 (Qoverflow_erro= r, build_string ("JSON string is too
> > long"));
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 return json_make_string (json_s= tring_value (json), size);
> >
> > Once again, the overflow test is redundant, as make_specified_str= ing
> > (called by json_make_string) already includes an equivalent test.=
>
> And once again, we need to check at least whether the size fits into > ptrdiff_t.
No, we don't, as we don't in other similar cases.I don't understand why you think these checks aren= 39;t necessary. Converting between integral types when the number is out of= range for the destination type results in an implementation-defined result= , i.e. it's unportable. Even assuming the GCC convention, performing su= ch conversions results in dangerously incorrect values.=C2=A0--94eb2c04faa2966310055aa67996--
> > > +=C2=A0 =C2=A0 case JSON_ARRAY:
> > > +=C2=A0 =C2=A0 =C2=A0 {
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (++lisp_eval_depth > max_= lisp_eval_depth)
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xsignal0 (Qjson_object_t= oo_deep);
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 size_t size =3D json_array_size= (json);
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (FIXNUM_OVERFLOW_P (size)) > > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xsignal1 (Qoverflow_erro= r, build_string ("JSON array is too
> > long"));
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 Lisp_Object result =3D Fmake_ve= ctor (make_natnum (size),
> > Qunbound);
> >
> > Likewise here: Fmake_vector makes sure the size is not larger tha= n
> > allowed.
>
> Same as above: It can't.
It can and it does.No, it can't. m= ake_natnum takes a ptrdiff_t argument, and when passing a value that's = out of range for ptrdiff_t, it will receive an incorrect, implementation-de= fined value.=C2=A0
> > > +=C2=A0 =C2=A0 case JSON_OBJECT:
> > > +=C2=A0 =C2=A0 =C2=A0 {
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (++lisp_eval_depth > max_= lisp_eval_depth)
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xsignal0 (Qjson_object_t= oo_deep);
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 size_t size =3D json_object_siz= e (json);
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 if (FIXNUM_OVERFLOW_P (size)) > > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xsignal1 (Qoverflow_erro= r,
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 build_string ("JSON object has too many elements")); > > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 Lisp_Object result =3D CALLN (F= make_hash_table, QCtest, Qequal,
> > > +=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 QCsize, = make_natnum (size));
> >
> > Likewise here: make_natnum does the equivalent test.
>
> It doesn't and can't.
Yes, it does:
=C2=A0 INLINE Lisp_Object
=C2=A0 make_natnum (EMACS_INT n)
=C2=A0 {
=C2=A0 =C2=A0 eassert (0 <=3D n && n <=3D MOST_POSITIVE_FIXNU= M);=C2=A0 <<<<<<<<<<<<<<<
=C2=A0 =C2=A0 EMACS_INT int0 =3D Lisp_Int0;=We're not talking about the same thing. What if make_natnum is cal= led with a value that doesn't fit in EMACS_INT?Also an asser= tion is incorrect here because the overflowing value comes from user data.<= /div>