TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: (CloudFlare bug) - Why does write() in C allow negative length?

3 pointsby winteriscomingabout 8 years ago
In context of the CloudFare fiasco, the bug report[1] states this<p>&gt;&gt; My current theory is that they had some code in their &quot;ScrapeShield&quot; feature that did something like this:<p>&gt;&gt; int Length = ObfuscateEmailAddressesInHtml(&amp;OutputBuffer, CachedPage);<p>&gt;&gt; write(fd, OutputBuffer, Length);<p>&gt;&gt; But they weren&#x27;t checking if the obfuscation parsers returned a negative value because of malformed HTML. This would explain the data I&#x27;m seeing.<p>I haven&#x27;t used C in my professional career (I mainly use Java), so the fact that this function allowed a negative value, for &quot;num bytes to write&quot;, to be passed to it and then even went on to write data without throwing an error for passing a negative value baffles me. I&#x27;m genuinely curious why this function doesn&#x27;t throw an error for negative &quot;num bytes to write&quot;. Is there any use case which I&#x27;m missing where it would be valid to send a negative value and expect it to write negative number of bytes?<p>[1] https:&#x2F;&#x2F;bugs.chromium.org&#x2F;p&#x2F;project-zero&#x2F;issues&#x2F;detail?id=1139

1 comment

technionabout 8 years ago
I&#x27;ve written a gist that somewhat explains this:<p><a href="https:&#x2F;&#x2F;gist.github.com&#x2F;technion&#x2F;a16095fa6e3a027d6bc938ad6f9bdc50" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;technion&#x2F;a16095fa6e3a027d6bc938ad6f9...</a><p>write() doesn&#x27;t &quot;print a negative number&quot;, as per the man page, write(2) takes a size_t for an argument. And what that means is explained if you run that gist:<p><pre><code> We have a count of 18446744073709551611 and will write that many characters </code></pre> In short, you&#x27;re looking at a signed, negative integer, cast to a very large positive integer. clang will only print a warning if you compile with -Wconversion and whilst I like to do that in my own code, most people find that too noisy.<p>Edit: Personally, I feel this &quot;email obfuscation feature&quot; should never have been written in the first place. There are a myriad of options for people to do this in their own website without something this obviously complex being built into an nginx module of a CDN.
评论 #13730672 未加载