<p>In <a href="https://github.com/geany/geany/pull/621#discussion_r40704938">src/document.c</a>:</p>
<pre style='color:#555'>>      {
>            ui_set_statusbar(TRUE, "%s", err->message);
>            g_error_free(err);
>            return FALSE;
>    }
>  
> -  filedata->len = (gsize) st.st_size;
</pre>
<p><a href="https://github.com/techee" class="user-mention">@techee</a> as <a href="https://github.com/elextr" class="user-mention">@elextr</a> said, you don't pass the <code>strlen()</code> value (so, truncated at the first <code>\0</code>), but the data size as read by <code>g_file_*_contents()</code> (which should include possible embedded <code>\0</code>s).  So it should effectively be the same as the <code>stat()</code> size, or if it's not anyway it would lead to an out-of-bounds read (which probably could happen currently if a race condition happens between the 2 calls and the file changes size in-between).</p>

<p>So no, I don't think you need to change this, as AFAIU, the stat value was never really documented as a better alternative to the <code>g_file_get_contents()</code> one (see my tracing back) and has glaring potential drawbacks (invalid memory access).</p>

<blockquote>
<p>Maybe we shouldn't store the length here at all if encoding stores strlen() as part of its NUL checks.</p>
</blockquote>

<p>Yes we need it, as encodings checks need the real length value, although currently it doesn't like it when it differs from <code>strlen()</code>, but has special handling especially for this case.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br>Reply to this email directly or <a href="https://github.com/geany/geany/pull/621/files#r40704938">view it on GitHub</a>.<img alt="" height="1" src="https://github.com/notifications/beacon/ABDrJ86ZB8SXZhnE_RXMGasCobgqBGsBks5o2sgSgaJpZM4FwLm6.gif" width="1" /></p>
<div itemscope itemtype="http://schema.org/EmailMessage">
<div itemprop="action" itemscope itemtype="http://schema.org/ViewAction">
  <link itemprop="url" href="https://github.com/geany/geany/pull/621/files#r40704938"></link>
  <meta itemprop="name" content="View Pull Request"></meta>
</div>
<meta itemprop="description" content="View this Pull Request on GitHub"></meta>
</div>