<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en">
<head>
<meta content="text/html; charset=US-ASCII" http-equiv="Content-Type">
<title>
GitLab
</title>


<style>img {
max-width: 100%; height: auto;
}
</style>
</head>
<body>
<div class="content">

<p class="details" style="font-style: italic; color: #777;">
<a href="https://gitlab.com/alexander-us">Alexander Us</a> created a merge request:
</p>
<p>
</p>
<div class="branch">
Project:Branches: alexander-us/libtasn1:clang_10_ptr_arith_ub to gnutls/libtasn1:master
</div>
<div class="author">
Author: Alexander Us
</div>
<div class="assignee">
Assignees: 
</div>
<div class="approvers">

</div>

<div>
<p dir="auto"><code>asn1_der_coding</code> contained unchecked pointer arithmetics.</p>
<p dir="auto">As source code shows, <code>ider</code> can be <code>NULL</code> pointer. C standard states that arithmetic using <code>NULL</code> pointers gives undefined behavior (C99, 6.5.6, clause 8). LLVM (beginning from version 10) started to optimize pointer arithmetic in comparisons with <code>NULL</code> with assumptions that addition of <code>NULL</code> and non-zero value will produce undefined behavior (<a href="https://reviews.llvm.org/D66608" rel="nofollow noreferrer noopener" target="_blank">https://reviews.llvm.org/D66608</a>). This means that code like <code>ptr + x == NULL</code> will be turned to <code>ptr == NULL</code> because if ptr is <code>NULL</code> then <code>NULL + x</code> will be UB. Short summary of what happened in <code>asn1_der_coding</code> is in this code: <a href="https://godbolt.org/z/DAo0Bt" rel="nofollow noreferrer noopener" target="_blank">https://godbolt.org/z/DAo0Bt</a>.</p>
<p dir="auto">Without my patch clang UBSan reports problems in <strong>copynode</strong> and <strong>Test_tree</strong> tests and these tests fail with <code>-O2</code> flag (<code>CC=clang-10 CFLAGS="-O2 -fsanitize=undefined"</code>):</p>
<pre class="code highlight js-syntax-highlight plaintext" lang="plaintext" v-pre="true"><code><span id="LC1" class="line" lang="plaintext">../../libtasn1/lib/coding.c:1207:56: runtime error: applying non-zero offset 7 to null pointer</span>
<span id="LC2" class="line" lang="plaintext">SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../libtasn1/lib/coding.c:1207:56 in</span>
<span id="LC3" class="line" lang="plaintext">../../libtasn1/lib/coding.c:1225:54: runtime error: applying non-zero offset 287 to null pointer</span>
<span id="LC4" class="line" lang="plaintext">SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ../../libtasn1/lib/coding.c:1225:54 in</span>
<span id="LC5" class="line" lang="plaintext">LIBTASN1 ERROR: VALUE_NOT_VALID</span>
<span id="LC6" class="line" lang="plaintext">Cannot copy node</span>
<span id="LC7" class="line" lang="plaintext">FAIL copynode (exit status: 1)</span></code></pre>
<h2 dir="auto">
<a id="user-content-checklist" class="anchor" href="#checklist" aria-hidden="true"></a>Checklist</h2>
<ul class="task-list" dir="auto">
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> Code modified for feature</li>
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> Test suite updated with functionality tests</li>
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> Test suite updated with negative tests</li>
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" checked disabled> Documentation updated</li>
</ul>
<h2 dir="auto">
<a id="user-content-reviewers-checklist" class="anchor" href="#reviewers-checklist" aria-hidden="true"></a>Reviewer's checklist:</h2>
<ul class="task-list" dir="auto">
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> There is a test suite reasonably covering new functionality or modifications</li>
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> Function naming, parameters, return values, types, etc., are consistent with other code</li>
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> This feature/change has adequate documentation added</li>
<li class="task-list-item">
<input type="checkbox" class="task-list-item-checkbox" disabled> No obvious mistakes in the code</li>
</ul>
</div>

</div>
<div class="footer" style="margin-top: 10px;">
<p style="font-size: small; color: #777;">

<br>
Reply to this email directly or <a href="https://gitlab.com/gnutls/libtasn1/-/merge_requests/63">view it on GitLab</a>.
<br>
You're receiving this email because of your account on gitlab.com.
If you'd like to receive fewer emails, you can
<a href="https://gitlab.com/-/sent_notifications/82b14cd067078875650693a04fc1fb4b/unsubscribe">unsubscribe</a>
from this thread or
adjust your notification settings.
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ViewAction","name":"View Merge request","url":"https://gitlab.com/gnutls/libtasn1/-/merge_requests/63"}}</script>


</p>
</div>
</body>
</html>