[Asterisk-code-review] main/say: Work around gcc 9 format-truncation false positive (asterisk[13])

Walter Doekes asteriskteam at digium.com
Wed Oct 28 09:25:21 CDT 2020


Walter Doekes has posted comments on this change. ( https://gerrit.asterisk.org/c/asterisk/+/14501 )

Change subject: main/say: Work around gcc 9 format-truncation false positive
......................................................................


Patch Set 1:

> At the end, it is ‘our’ job to create a minimal example and then hook-up with the GCC community.

Well.. here's a trimmed down example. One might be able to trim it some more, but probably not much.

```c

#include <limits.h>
#include <string.h>
#include <stdio.h>

int ast_say_number_full_zh(int num)
{
    int res = 0;
    int playh = 0;
    int playt = 0;
    int playz = 0;
    int last_length = 0;
    char buf[20] = "";
    char fn[256] = "";

    while (!res && (num || playh || playt || playz)) {
        if (num < 0) {
            if ( num > INT_MIN ) {
                num = -num;
            } else {
                num = 0;
            }
#if 1 /* disable this to fix the false positive */
        } else if (playz) {
            snprintf(fn, sizeof(fn), "digits/0");
            last_length = 0;
            playz = 0;
        } else if (playh) {
            playh = 0;
        } else if (playt) {
            snprintf(fn, sizeof(fn), "digits/thousand");
            playt = 0;
#endif                
        } else if (num < 10) {
#if 0
            snprintf(buf, 10, "%hhd", (char)num);
#else
            snprintf(buf, 10, "%d", num);
#endif
            num = 0;
        } else if (num < 1000){
            snprintf(buf, 10, "%d", num);
            if (last_length - strlen(buf) > 1 && last_length != 0) {
                last_length = strlen(buf);
                playz++;
                continue;
            }
            snprintf(fn, sizeof(fn), "digits/%d", (num / 100));
            playh++;
            snprintf(buf, 10, "%d", num);
            last_length = strlen(buf);
            num -= ((num / 100) * 100);
        } else {
            res = -1;
        }
    }
    return res;
}

```

Compiling anywhere:
```
$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0

$ gcc -O1 -mtune=nocona -S -Wall say2.c
say2.c: In function ‘ast_say_number_full_zh’:
say2.c:37:32: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 10 [-Wformat-truncation=]
   37 |             snprintf(buf, 10, "%d", num);
```

For -mtune=haswell (detected on my desktop), no False Positive occurs.


-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/14501
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Change-Id: Ic7a70120188c9aa525a6d70289385bfce878438a
Gerrit-Change-Number: 14501
Gerrit-PatchSet: 1
Gerrit-Owner: Walter Doekes <walter+asterisk at wjd.nu>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-CC: Alexander Traud <pabstraud at compuserve.com>
Gerrit-Comment-Date: Wed, 28 Oct 2020 14:25:21 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20201028/d85fbadf/attachment.html>


More information about the asterisk-code-review mailing list