Re: cdbmake key length and the magic number 429496720

James Craig Burley <[email protected]> Fri, 05 Oct 2018 13:47:58 -0700
Newsgroups gmane.comp.djb.cdb
Message-ID <[email protected]>
------ZOKP5OBMNMOLLTK7RTQDDKNBHBTB56
Content-Type: text/plain;
 charset=utf-8
Content-Transfer-Encoding: quoted-printable

You're on the right track=2E The comparison is made just before the new dig=
it is "appended", while klen is still safely comparable as a 32-bit unsigne=
d value=2E

While not precise, that check protects against a key length nearly, or gre=
ater than, 4GB=2E

On October 5, 2018 9:07:30 AM PDT, Profpatsch <mail@profpatsch=2Ede> wrote=
:
>
>cdbmake has the following code snippet for parsing in
>the length of keys formatted as ASCII numbers:
>
>klen =3D 0;
>for (;;) {
>  get(&ch);
>  if (ch =3D=3D ',') break;
>  if ((ch < '0') || (ch > '9')) die_readformat();
>  if (klen > 429496720) { errno =3D error_nomem; die_write(); }
>  klen =3D klen * 10 + (ch - '0');
>}
>
>As you can observe, there is an undocumented magic number=2E
>I can=E2=80=99t make sense of that number=2E
>
>  logBase2(429496720) ~ 28=2E67
>
>Maybe 4GB limit
>
>  2^30*4 =3D=3D 4294967296
>         !=3D 429496720
>
>I am confounded=2E
>
>
>  Profpatsch
>
>--=20
>Written with Emacs (mu4e) on NixOS=2E
>Q: Why is this email five sentences or less?
>A: http://five=2Esentenc=2Ees/
>May take up to five days to read your message=2E If it=E2=80=99s urgent, =
call me=2E

------ZOKP5OBMNMOLLTK7RTQDDKNBHBTB56
Content-Type: text/html;
 charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html><head></head><body>You're on the right track=2E The comparison is mad=
e just before the new digit is "appended", while klen is still safely compa=
rable as a 32-bit unsigned value=2E<br><br>While not precise, that check pr=
otects against a key length nearly, or greater than, 4GB=2E<br><br><div cla=
ss=3D"gmail_quote">On October 5, 2018 9:07:30 AM PDT, Profpatsch &lt;mail@p=
rofpatsch=2Ede&gt; wrote:<blockquote class=3D"gmail_quote" style=3D"margin:=
 0pt 0pt 0pt 0=2E8ex; border-left: 1px solid rgb(204, 204, 204); padding-le=
ft: 1ex;">
<pre class=3D"k9mail"><br>cdbmake has the following code snippet for parsi=
ng in<br>the length of keys formatted as ASCII numbers:<br><br>klen =3D 0;<=
br>for (;;) {<br>  get(&amp;ch);<br>  if (ch =3D=3D ',') break;<br>  if ((c=
h &lt; '0') || (ch &gt; '9')) die_readformat();<br>  if (klen &gt; 42949672=
0) { errno =3D error_nomem; die_write(); }<br>  klen =3D klen * 10 + (ch - =
'0');<br>}<br><br>As you can observe, there is an undocumented magic number=
=2E<br>I can=E2=80=99t make sense of that number=2E<br><br>  logBase2(42949=
6720) ~ 28=2E67<br><br>Maybe 4GB limit<br><br>  2^30*4 =3D=3D 4294967296<br=
>         !=3D 429496720<br><br>I am confounded=2E<br><br><br>  Profpatsch<=
br></pre></blockquote></div></body></html>
------ZOKP5OBMNMOLLTK7RTQDDKNBHBTB56--