<p>fixeria has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/c/libosmocore/+/22542">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">gsm_7bit_encode_n(): test encoding of more than 250 septets<br><br>As can be seen, this unit test reveals problems with encoding<br>of more than 250 septets using gsm_7bit_encode_n().  The problem<br>is that some API functions use type 'uint8_t' for the length, so<br>we basically suffer from integer overflows.<br><br>Change-Id: I723300578d5ab0c7b94cf49c14d962b2dbf47740<br>---<br>M tests/sms/sms_test.c<br>M tests/sms/sms_test.ok<br>2 files changed, 70 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/42/22542/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/tests/sms/sms_test.c b/tests/sms/sms_test.c</span><br><span>index 0615396..c7f47e2 100644</span><br><span>--- a/tests/sms/sms_test.c</span><br><span>+++ b/tests/sms/sms_test.c</span><br><span>@@ -268,6 +268,54 @@</span><br><span>  printf("Result: len(%d) data(%s)\n", len, osmo_hexdump(oa, len));</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static void test_enc_large_msg(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ uint8_t enc_buf[2048 * 7 / 8];</span><br><span style="color: hsl(120, 100%, 40%);">+        char large_msg[2048 + 1];</span><br><span style="color: hsl(120, 100%, 40%);">+     int i, j, nsep, noct = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   printf("\nRunning %s\n", __func__);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       /* Expected chunks (repeated) in the output buffer */</span><br><span style="color: hsl(120, 100%, 40%);">+ const uint8_t exp_chunk[] = { 0xc1, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x83 };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   /* Length variants to be tested */</span><br><span style="color: hsl(120, 100%, 40%);">+    static const size_t nlen[] = { 2048, 1024, 555, 512, 260, 255, 250 };</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       memset(&large_msg[0], (int) 'A', sizeof(large_msg) - 1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        for (i = 0; i < ARRAY_SIZE(nlen); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+           /* Clear the output buffer first */</span><br><span style="color: hsl(120, 100%, 40%);">+           memset(&enc_buf[0], 0x00, sizeof(enc_buf));</span><br><span style="color: hsl(120, 100%, 40%);">+               /* Limit length of the input string */</span><br><span style="color: hsl(120, 100%, 40%);">+                large_msg[nlen[i]] = '\0';</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+          /* How many octets we expect to be used? */</span><br><span style="color: hsl(120, 100%, 40%);">+           int noct_exp = nlen[i] * 7 / 8;</span><br><span style="color: hsl(120, 100%, 40%);">+               if (nlen[i] % 8 != 0)</span><br><span style="color: hsl(120, 100%, 40%);">+                 noct_exp++;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+         /* Encode a sequence of 'A' repeated nlen[i] times */</span><br><span style="color: hsl(120, 100%, 40%);">+         nsep = gsm_7bit_encode_n(&enc_buf[0], sizeof(enc_buf), large_msg, &noct);</span><br><span style="color: hsl(120, 100%, 40%);">+             printf("gsm_7bit_encode_n(len=%zu) processed %d septets (expected %zu): %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                     nlen[i], nsep, nlen[i], nsep == nlen[i] ? "OK" : "FAIL");</span><br><span style="color: hsl(120, 100%, 40%);">+          printf("gsm_7bit_encode_n(len=%zu) used %d octets in the buffer (expected %d): %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                      nlen[i], noct, noct_exp, noct == noct_exp ? "OK" : "FAIL");</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+              /* The encoding result is expected to consist of repeated chunks */</span><br><span style="color: hsl(120, 100%, 40%);">+           for (j = 0; j < noct_exp; j += sizeof(exp_chunk)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        size_t len = OSMO_MIN(noct_exp - j, sizeof(exp_chunk));</span><br><span style="color: hsl(120, 100%, 40%);">+                       if (nlen[i] % 8 != 0) /* skip incomplete octets */</span><br><span style="color: hsl(120, 100%, 40%);">+                            len--;</span><br><span style="color: hsl(120, 100%, 40%);">+                        if (memcmp(&enc_buf[j], exp_chunk, len) != 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+                           printf("\tUnexpected chunk at enc_buf[%d:%zu]: %s\n",</span><br><span style="color: hsl(120, 100%, 40%);">+                                      j, len, osmo_hexdump(&enc_buf[j], len));</span><br><span style="color: hsl(120, 100%, 40%);">+                           break; /* No need to show them all */</span><br><span style="color: hsl(120, 100%, 40%);">+                 }</span><br><span style="color: hsl(120, 100%, 40%);">+             }</span><br><span style="color: hsl(120, 100%, 40%);">+     }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int main(int argc, char** argv)</span><br><span> {</span><br><span>       printf("SMS testing\n");</span><br><span>@@ -396,6 +444,7 @@</span><br><span> </span><br><span>         test_octet_return();</span><br><span>         test_gen_oa();</span><br><span style="color: hsl(120, 100%, 40%);">+        test_enc_large_msg();</span><br><span> </span><br><span>    printf("OK\n");</span><br><span>    return 0;</span><br><span>diff --git a/tests/sms/sms_test.ok b/tests/sms/sms_test.ok</span><br><span>index a71567d..724c166 100644</span><br><span>--- a/tests/sms/sms_test.ok</span><br><span>+++ b/tests/sms/sms_test.ok</span><br><span>@@ -18,4 +18,25 @@</span><br><span> Result: len(2) data(00 91 )</span><br><span> Result: len(9) data(0e d0 4f 78 d9 2d 9c 0e 01 )</span><br><span> Result: len(12) data(14 d0 4f 78 d9 2d 9c 0e c3 e2 31 19 )</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Running test_enc_large_msg</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=2048) processed 2048 septets (expected 2048): OK</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=2048) used 0 octets in the buffer (expected 1792): FAIL</span><br><span style="color: hsl(120, 100%, 40%);">+ Unexpected chunk at enc_buf[0:7]: 00 00 00 00 00 00 00 </span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=1024) processed 1024 septets (expected 1024): OK</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=1024) used 0 octets in the buffer (expected 896): FAIL</span><br><span style="color: hsl(120, 100%, 40%);">+ Unexpected chunk at enc_buf[0:7]: 00 00 00 00 00 00 00 </span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=555) processed 555 septets (expected 555): OK</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=555) used 38 octets in the buffer (expected 486): FAIL</span><br><span style="color: hsl(120, 100%, 40%);">+    Unexpected chunk at enc_buf[35:6]: c1 60 10 00 00 00 </span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=512) processed 512 septets (expected 512): OK</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=512) used 0 octets in the buffer (expected 448): FAIL</span><br><span style="color: hsl(120, 100%, 40%);">+       Unexpected chunk at enc_buf[0:7]: 00 00 00 00 00 00 00 </span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=260) processed 260 septets (expected 260): OK</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=260) used 4 octets in the buffer (expected 228): FAIL</span><br><span style="color: hsl(120, 100%, 40%);">+     Unexpected chunk at enc_buf[0:6]: c1 60 30 08 00 00 </span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=255) processed 255 septets (expected 255): OK</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=255) used 224 octets in the buffer (expected 224): OK</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=250) processed 250 septets (expected 250): OK</span><br><span style="color: hsl(120, 100%, 40%);">+gsm_7bit_encode_n(len=250) used 219 octets in the buffer (expected 219): OK</span><br><span> OK</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/libosmocore/+/22542">change 22542</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/libosmocore/+/22542"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I723300578d5ab0c7b94cf49c14d962b2dbf47740 </div>
<div style="display:none"> Gerrit-Change-Number: 22542 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: fixeria <vyanitskiy@sysmocom.de> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>