From 2a429fde26212e8c8c9a5a44f9a4a06ee60a5208 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Tue, 5 Jan 2021 23:09:49 +0100
Subject: Fortified the d2c compiler by checking asprint() return values.

---
 tools/d2c/args/tokens.l    |  12 ++-
 tools/d2c/assert/tokens.l  |  12 ++-
 tools/d2c/bits/tokens.l    |  12 ++-
 tools/d2c/coder.c          | 208 +++++++++++++++++++++++++++++++++++----------
 tools/d2c/conv/tokens.l    |  12 ++-
 tools/d2c/d2c.c            |  10 ++-
 tools/d2c/encoding.c       |  59 +++++++++----
 tools/d2c/format/tokens.l  |  12 ++-
 tools/d2c/hooks/tokens.l   |  12 ++-
 tools/d2c/id/tokens.l      |  12 ++-
 tools/d2c/pattern/tokens.l |  12 ++-
 tools/d2c/rules/tokens.l   |  12 ++-
 tools/d2c/tokens.l         |  14 ++-
 13 files changed, 306 insertions(+), 93 deletions(-)

diff --git a/tools/d2c/args/tokens.l b/tools/d2c/args/tokens.l
index 9645ba2..eda5a5c 100644
--- a/tools/d2c/args/tokens.l
+++ b/tools/d2c/args/tokens.l
@@ -73,9 +73,15 @@
 
 .                           {
                                 char *msg;
-                                asprintf(&msg, "Unhandled token in d2c args block: '%s'", yytext);
-                                YY_FATAL_ERROR(msg);
-                                free(msg);
+                                int ret;
+                                ret = asprintf(&msg, "Unhandled token in d2c args block: '%s'", yytext);
+                                if (ret == -1)
+                                    YY_FATAL_ERROR("Unhandled token in undisclosed d2c args block");
+                                else
+                                {
+                                    YY_FATAL_ERROR(msg);
+                                    free(msg);
+                                }
                             }
 
 
diff --git a/tools/d2c/assert/tokens.l b/tools/d2c/assert/tokens.l
index e455a76..4a67f5a 100644
--- a/tools/d2c/assert/tokens.l
+++ b/tools/d2c/assert/tokens.l
@@ -33,9 +33,15 @@
 
 .                       {
                             char *msg;
-                            asprintf(&msg, "Unhandled token in d2c assert block: '%s'", yytext);
-                            YY_FATAL_ERROR(msg);
-                            free(msg);
+                            int ret;
+                            ret = asprintf(&msg, "Unhandled token in d2c assert block: '%s'", yytext);
+                            if (ret == -1)
+                                YY_FATAL_ERROR("Unhandled token in undisclosed d2c assert block");
+                            else
+                            {
+                                YY_FATAL_ERROR(msg);
+                                free(msg);
+                            }
                         }
 
 %%
diff --git a/tools/d2c/bits/tokens.l b/tools/d2c/bits/tokens.l
index 4879f4f..deac2c7 100644
--- a/tools/d2c/bits/tokens.l
+++ b/tools/d2c/bits/tokens.l
@@ -31,9 +31,15 @@
 
 .                       {
                             char *msg;
-                            asprintf(&msg, "Unhandled token in d2c bits block: '%s'", yytext);
-                            YY_FATAL_ERROR(msg);
-                            free(msg);
+                            int ret;
+                            ret = asprintf(&msg, "Unhandled token in d2c bits block: '%s'", yytext);
+                            if (ret == -1)
+                                YY_FATAL_ERROR("Unhandled token in undisclosed d2c bits block");
+                            else
+                            {
+                                YY_FATAL_ERROR(msg);
+                                free(msg);
+                            }
                         }
 
 %%
diff --git a/tools/d2c/coder.c b/tools/d2c/coder.c
index be68cc5..0955c86 100644
--- a/tools/d2c/coder.c
+++ b/tools/d2c/coder.c
@@ -302,12 +302,19 @@ void save_notes_for_coder(rented_coder *coder, char *copy, char *ins, char sep,
 char *get_coder_nominal_name(const rented_coder *coder)
 {
     char *result;                           /* Désignation à retourner     */
+    int ret;                                /* Bilan de construction       */
 
     if (coder->separator == '\0')
         result = strdup(coder->ins);
 
     else
-        asprintf(&result, "%s%c%s", coder->ins, coder->separator, coder->raw_details);
+    {
+        ret = asprintf(&result, "%s%c%s", coder->ins, coder->separator, coder->raw_details);
+
+        if (ret == -1)
+            result = NULL;
+
+    }
 
     return result;
 
@@ -330,6 +337,7 @@ char *get_coder_code_name(const rented_coder *coder)
     char *result;                           /* Désignation à retourner     */
     char *keyword;                          /* Mot clef appelable en code  */
     char *details;                          /* Compléments de distinction  */
+    int ret;                                /* Bilan de construction       */
 
     keyword = make_callable(coder->ins, false);
 
@@ -340,12 +348,15 @@ char *get_coder_code_name(const rented_coder *coder)
     {
         details = make_callable(coder->raw_details, true);
 
-        asprintf(&result, "%s%s", keyword, details);
+        ret = asprintf(&result, "%s%s", keyword, details);
 
         free(keyword);
 
         free(details);
 
+        if (ret == -1)
+            result = NULL;
+
     }
 
     return result;
@@ -538,6 +549,9 @@ char *build_coder_main_identifier(const rented_coder *coder, const output_info *
     char *filename;                         /* Nom de fichier modifiable   */
     char *sub;                              /* Compartimentage à insérer   */
     char *name;                             /* Désignation à manipuler     */
+    int ret;                                /* Bilan d'une construction    */
+
+    result = NULL;
 
     if (info->filename_reuse > 0)
     {
@@ -551,15 +565,22 @@ char *build_coder_main_identifier(const rented_coder *coder, const output_info *
     }
 
     name = get_coder_code_name(coder);
+    if (name == NULL) goto exit;
+
     make_string_upper(name);
 
     if (info->filename_reuse > 0)
-        asprintf(&result, "%s_%s_%s", info->id_prefix, sub, name);
+        ret = asprintf(&result, "%s_%s_%s", info->id_prefix, sub, name);
     else
-        asprintf(&result, "%s_%s", info->id_prefix, name);
+        ret = asprintf(&result, "%s_%s", info->id_prefix, name);
 
     free(name);
 
+    if (ret == -1)
+        result = NULL;
+
+ exit:
+
     if (info->filename_reuse > 0)
         free(filename);
 
@@ -600,12 +621,18 @@ static int open_code_file(const rented_coder *coder, const char *path, const cha
         *sep = '\0';
 
     if (prefix != NULL)
-        asprintf(&pathname, "%s%s_%s.c", path, prefix, group);
+        ret = asprintf(&pathname, "%s%s_%s.c", path, prefix, group);
     else
-        asprintf(&pathname, "%s%s.c", path, group);
+        ret = asprintf(&pathname, "%s%s.c", path, group);
 
     free(group);
 
+    if (ret == -1)
+    {
+        result = -1;
+        goto exit;
+    }
+
     ret = access(pathname, F_OK);
 
     *new = (ret != 0);
@@ -620,6 +647,8 @@ static int open_code_file(const rented_coder *coder, const char *path, const cha
 
     free(pathname);
 
+ exit:
+
     return result;
 
 }
@@ -647,12 +676,16 @@ static int open_header_file(const char *path, const char *prefix, const char *na
     int ret;                                /* Test d'existence du fichier */
     int flags;                              /* Mode d'accès au fichier     */
 
-    asprintf(&pathname, "%s%s.h", path, name);
-
     if (prefix != NULL)
-        asprintf(&pathname, "%s%s_%s.h", path, prefix, name);
+        ret = asprintf(&pathname, "%s%s_%s.h", path, prefix, name);
     else
-        asprintf(&pathname, "%s%s.h", path, name);
+        ret = asprintf(&pathname, "%s%s.h", path, name);
+
+    if (ret == -1)
+    {
+        result = -1;
+        goto exit;
+    }
 
     ret = access(pathname, F_OK);
 
@@ -668,6 +701,8 @@ static int open_header_file(const char *path, const char *prefix, const char *na
 
     free(pathname);
 
+ exit:
+
     return result;
 
 }
@@ -872,6 +907,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
     size_t j;                               /* Boucle de parcours #2       */
     int header_fd;                          /* Fichier de déclarations     */
     char *file;                             /* Nom de fichier final        */
+    int ret;                                /* Bilan d'une construction    */
     bool header_new;                        /* Note une création d'entête  */
     int code_fd;                            /* Fichier de définitions      */
     bool code_new;                          /* Note une création de code   */
@@ -882,7 +918,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
     {
         enc_name = find_encoding(coder->pp, i);
 
-        for (j = 0; j < coder->specs_count; j++)
+        for (j = 0; j < coder->specs_count && result; j++)
         {
             /* On s'assure qu'il existe bien une version pour l'encodage visé... */
             if (!has_encoding_spec_prefix(coder->specs[j], enc_name->src))
@@ -892,7 +928,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
             if (header_fd == -1)
             {
                 result = false;
-                goto ocb_exit;
+                break;
             }
 
             if (header_new)
@@ -900,7 +936,16 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
                 if (enc_name->dest == NULL)
                     file = strdup("opcodes");
                 else
-                    asprintf(&file, "%s_opcodes", enc_name->dest);
+                {
+                    ret = asprintf(&file, "%s_opcodes", enc_name->dest);
+
+                    if (ret == -1)
+                    {
+                        result = false;
+                        goto close_header;
+                    }
+
+                }
 
                 write_header_file_license(header_fd, info, file, "prototypes pour la traduction d'instructions");
 
@@ -914,7 +959,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
             if (code_fd == -1)
             {
                 result = false;
-                goto ocb_exit;
+                goto close_header;
             }
 
             if (code_new)
@@ -922,7 +967,16 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
                 if (enc_name->dest == NULL)
                     file = strdup(coder->ins);
                 else
-                    asprintf(&file, "%s_%s", enc_name->dest, coder->ins);
+                {
+                    ret = asprintf(&file, "%s_%s", enc_name->dest, coder->ins);
+
+                    if (ret == -1)
+                    {
+                        result = false;
+                        goto close_code;
+                    }
+
+                }
 
                 write_code_file_license(code_fd, info, file, coder->copyright);
 
@@ -953,6 +1007,14 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
 
             }
 
+ close_code:
+
+            close(code_fd);
+
+ close_header:
+
+            close(header_fd);
+
         }
 
         /* La suite ne concerne que les formats bruts aboutis... */
@@ -969,7 +1031,7 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
             if (header_fd == -1)
             {
                 result = false;
-                goto ocb_exit;
+                break;
             }
 
             assert(!header_new);
@@ -978,21 +1040,23 @@ bool output_coder_body(const rented_coder *coder, const output_info *info)
             if (code_fd == -1)
             {
                 result = false;
-                goto ocb_exit;
+                close(header_fd);
+                break;
             }
 
             assert(!code_new);
 
             result = output_coder_main_raw(coder, info, enc_name, header_fd, code_fd);
 
+            close(code_fd);
+            close(header_fd);
+
             break;
 
         }
 
     }
 
- ocb_exit:
-
     return result;
 
 }
@@ -1039,6 +1103,7 @@ static void write_read_function_name(const rented_coder *coder, const output_inf
     }
 
     name = get_coder_code_name(coder);
+    if (name == NULL) goto exit;
 
     /* Impressions */
 
@@ -1056,6 +1121,8 @@ static void write_read_function_name(const rented_coder *coder, const output_inf
 
     free(name);
 
+ exit:
+
     if (info->filename_reuse > 0)
         free(filename);
 
@@ -1096,6 +1163,9 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info,
 
     prefix = build_encoding_spec_prefix(encoding);
 
+    result = (prefix != NULL);
+    if (!result) goto exit;
+
     bits = get_bits_in_encoding_spec(encoding);
     wide = count_coded_bits(bits);
 
@@ -1157,6 +1227,11 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info,
     dprintf(cfd, "\n");
 
     constant = build_coder_main_identifier(coder, info);
+    if (constant == NULL)
+    {
+        result = false;
+        goto exit;
+    }
 
     result = write_encoding_spec_raw_disass(encoding, cfd, arch, constant, coder->pp);
 
@@ -1167,6 +1242,8 @@ static bool output_coder_raw(const rented_coder *coder, const output_info *info,
 
     /* Conclusion */
 
+ exit:
+
     free(prefix);
 
     free(arch);
@@ -1263,10 +1340,11 @@ static bool output_coder_main_raw(const rented_coder *coder, const output_info *
         if (!has_encoding_spec_prefix(coder->specs[i], enc_name->src))
             continue;
 
-        result = true;
-
         prefix = build_encoding_spec_prefix(coder->specs[i]);
 
+        result = (prefix != NULL);
+        if (!result) break;
+
         if (first)
         {
             dprintf(cfd, "\tresult = ");
@@ -1387,6 +1465,11 @@ static bool output_coder_format(const rented_coder *coder, const output_info *in
     dprintf(cfd, "\n");
 
     constant = build_coder_main_identifier(coder, info);
+    if (constant == NULL)
+    {
+        result = false;
+        goto exit;
+    }
 
     result = write_encoding_spec_format_disass(encoding, cfd, arch, constant, info->fmt_prefix);
 
@@ -1397,6 +1480,8 @@ static bool output_coder_format(const rented_coder *coder, const output_info *in
 
     /* Conclusion */
 
+ exit:
+
     free(arch);
 
     return result;
@@ -1507,6 +1592,7 @@ bool output_coder_identifier(const rented_coder *coder, const output_info *info)
     instr_id *id;                           /* Gestionnaire d'identifiant  */
     unsigned int iid;                       /* Identifiant unique attribué */
     char *comment;                          /* Contenu du commentaire      */
+    int ret;                                /* Bilan d'une construction    */
     char *aligned;                          /* Adaptation pour l'alignement*/
 
     result = false;
@@ -1514,7 +1600,7 @@ bool output_coder_identifier(const rented_coder *coder, const output_info *info)
     /* Ouverture de la destination */
 
     fd = open_header_file(info->opcodes_dir, NULL, "identifiers", &created);
-    if (fd == -1) goto oci_exit;
+    if (fd == -1) goto exit;
 
     if (created)
     {
@@ -1522,35 +1608,52 @@ bool output_coder_identifier(const rented_coder *coder, const output_info *info)
         init_coder_identifiers_file(fd, info);
     }
 
-    /* Constitution de la constante */
-
-    constant = build_coder_main_identifier(coder, info);
-
     /* Définition du commentaire */
 
     name = get_coder_nominal_name(coder);
 
+    if (name == NULL)
+        goto failure_1;
+
     id = get_coder_instruction_id(coder);
     iid = get_instruction_id_value(id);
 
-    asprintf(&comment, "%s (0x%0*x)", name, info->id_len, iid);
+    ret = asprintf(&comment, "%s (0x%0*x)", name, info->id_len, iid);
 
     free(name);
 
-    /* Impression de la ligne */
+    if (ret == -1)
+        goto failure_1;
+
+    /* Constitution de la constante et impression de la ligne */
 
-    asprintf(&aligned, "%s,", constant);
+    constant = build_coder_main_identifier(coder, info);
+
+    if (constant == NULL)
+        goto failure_2;
+
+    ret = asprintf(&aligned, "%s,", constant);
+
+    free(constant);
+
+    if (ret == -1)
+        goto failure_2;
 
     dprintf(fd, "    %-40s/* %-28s*/\n", aligned, comment);
 
     free(aligned);
 
-    free(constant);
+    result = true;
+
+ failure_2:
+
     free(comment);
 
-    result = true;
+ failure_1:
+
+    close(fd);
 
- oci_exit:
+ exit:
 
     return result;
 
@@ -1659,7 +1762,7 @@ bool output_coder_sub_identifier(const rented_coder *coder, const output_info *i
     /* Ouverture de la destination */
 
     fd = open_header_file(info->opcodes_dir, NULL, "subidentifiers", &created);
-    if (fd == -1) goto ocsi_exit;
+    if (fd == -1) goto exit;
 
     if (created)
     {
@@ -1670,6 +1773,7 @@ bool output_coder_sub_identifier(const rented_coder *coder, const output_info *i
     /* Impression des sous-identifiants */
 
     constant = build_coder_main_identifier(coder, info);
+    if (constant == NULL) goto exit;
 
     result = true;
 
@@ -1678,7 +1782,7 @@ bool output_coder_sub_identifier(const rented_coder *coder, const output_info *i
 
     free(constant);
 
- ocsi_exit:
+ exit:
 
     return result;
 
@@ -1818,13 +1922,13 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info)
     if (coder->useless)
     {
         result = true;
-        goto ock_exit;
+        goto exit;
     }
 
     /* Ouverture de la destination */
 
     fd = open_header_file(info->opcodes_dir, NULL, "keywords", &created);
-    if (fd == -1) goto ock_exit;
+    if (fd == -1) goto exit;
 
     if (created)
     {
@@ -1835,6 +1939,7 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info)
     /* Lancement des impressions */
 
     constant = build_coder_main_identifier(coder, info);
+    if (constant == NULL) goto failure;
 
     result = true;
 
@@ -1861,9 +1966,15 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info)
 
                 name = get_coder_nominal_name(coder);
 
-                dprintf(fd, "\"%s\",\n", name);
+                result = (name != NULL);
 
-                free(name);
+                if (result)
+                {
+                    dprintf(fd, "\"%s\",\n", name);
+
+                    free(name);
+
+                }
 
                 break;
 
@@ -1871,7 +1982,11 @@ bool output_coder_keyword(const rented_coder *coder, const output_info *info)
 
     free(constant);
 
- ock_exit:
+ failure:
+
+    close(fd);
+
+ exit:
 
     return result;
 
@@ -2012,13 +2127,13 @@ bool output_coder_hooks(const rented_coder *coder, const output_info *info)
     if (coder->useless)
     {
         result = true;
-        goto och_exit;
+        goto exit;
     }
 
     /* Ouverture de la destination */
 
     fd = open_header_file(info->opcodes_dir, NULL, "hooks", &created);
-    if (fd == -1) goto och_exit;
+    if (fd == -1) goto exit;
 
     if (created)
     {
@@ -2029,6 +2144,7 @@ bool output_coder_hooks(const rented_coder *coder, const output_info *info)
     /* Lancement des impressions */
 
     constant = build_coder_main_identifier(coder, info);
+    if (constant == NULL) goto exit;
 
     result = true;
 
@@ -2053,7 +2169,7 @@ bool output_coder_hooks(const rented_coder *coder, const output_info *info)
 
     free(constant);
 
- och_exit:
+ exit:
 
     return result;
 
@@ -2174,13 +2290,13 @@ bool output_coder_description(const rented_coder *coder, const output_info *info
     if (coder->useless)
     {
         result = true;
-        goto ocd_exit;
+        goto exit;
     }
 
     /* Ouverture de la destination */
 
     fd = open_header_file(info->opcodes_dir, NULL, "descriptions", &created);
-    if (fd == -1) goto ocd_exit;
+    if (fd == -1) goto exit;
 
     if (created)
     {
@@ -2191,6 +2307,7 @@ bool output_coder_description(const rented_coder *coder, const output_info *info
     /* Impression de la colonne */
 
     constant = build_coder_main_identifier(coder, info);
+    if (constant == NULL) goto exit;
 
     dprintf(fd, "\t[%s] = ", constant);
 
@@ -2199,6 +2316,7 @@ bool output_coder_description(const rented_coder *coder, const output_info *info
     /* Impression du mot clef */
 
     name = get_coder_nominal_name(coder);
+    if (name == NULL) goto exit;
 
     dprintf(fd, "\"");
 
@@ -2210,7 +2328,7 @@ bool output_coder_description(const rented_coder *coder, const output_info *info
 
     result = true;
 
- ocd_exit:
+ exit:
 
     return result;
 
diff --git a/tools/d2c/conv/tokens.l b/tools/d2c/conv/tokens.l
index f1d196d..385f044 100644
--- a/tools/d2c/conv/tokens.l
+++ b/tools/d2c/conv/tokens.l
@@ -30,9 +30,15 @@
 
 .                       {
                             char *msg;
-                            asprintf(&msg, "Unhandled token in d2c conv block: '%s'", yytext);
-                            YY_FATAL_ERROR(msg);
-                            free(msg);
+                            int ret;
+                            ret = asprintf(&msg, "Unhandled token in d2c conv block: '%s'", yytext);
+                            if (ret == -1)
+                                YY_FATAL_ERROR("Unhandled token in undisclosed d2c conv block");
+                            else
+                            {
+                                YY_FATAL_ERROR(msg);
+                                free(msg);
+                            }
                         }
 
 
diff --git a/tools/d2c/d2c.c b/tools/d2c/d2c.c
index fbf401e..96bb81a 100644
--- a/tools/d2c/d2c.c
+++ b/tools/d2c/d2c.c
@@ -198,7 +198,15 @@ int main(int argc, char **argv)
                 break;
 
             case 'o':
-                asprintf(&info.opcodes_dir, "%sopcodes%c", optarg, optarg[strlen(optarg) - 1]);
+
+                ret = asprintf(&info.opcodes_dir, "%sopcodes%c", optarg, optarg[strlen(optarg) - 1]);
+
+                if (ret == -1)
+                {
+                    info.opcodes_dir = NULL;
+                    fprintf(stderr, "unable to memorize the specified main output directory; exiting...\n");
+                    goto exit;
+                }
                 break;
 
             case 't':
diff --git a/tools/d2c/encoding.c b/tools/d2c/encoding.c
index f8b72d9..9fbf0df 100644
--- a/tools/d2c/encoding.c
+++ b/tools/d2c/encoding.c
@@ -187,10 +187,14 @@ bool has_encoding_spec_prefix(const encoding_spec *spec, const char *prefix)
 char *build_encoding_spec_prefix(const encoding_spec *spec)
 {
     char *result;                           /* Chaîne à retourner          */
+    int ret;                                /* Recette de construction     */
 
     assert(spec->lprefix);
 
-    asprintf(&result, "%s%u", spec->lprefix, spec->index);
+    ret = asprintf(&result, "%s%u", spec->lprefix, spec->index);
+
+    if (ret == -1)
+        result = NULL;
 
     return result;
 
@@ -328,6 +332,7 @@ bool write_encoding_spec_raw_disass(const encoding_spec *spec, int fd, const cha
     disass_assert *dassert;                 /* Eventuelles conditions      */
     char *suffix;                           /* Complément d'identifiant    */
     char *sid;                              /* Base de sous-identifiant    */
+    int ret;                                /* Bilan d'une construction    */
     size_t i;                               /* Boucle de parcours          */
     bool op_decl;                           /* Suivi des déclaration #1    */
     bool imm_decl;                          /* Suivi des déclaration #2    */
@@ -365,7 +370,7 @@ bool write_encoding_spec_raw_disass(const encoding_spec *spec, int fd, const cha
     }
 
     if (!result)
-        goto wesrd_exit;
+        goto exit;
 
     /* Déclarations préalables */
 
@@ -375,23 +380,23 @@ bool write_encoding_spec_raw_disass(const encoding_spec *spec, int fd, const cha
         result = mark_syntax_items(spec->syntaxes[i], spec->bits);
 
     if (!result)
-        goto wesrd_exit;
+        goto exit;
 
     result = declare_used_bits_fields(spec->bits, fd);
-    if (!result) goto wesrd_exit;
+    if (!result) goto exit;
 
     if (openbar)
     {
         result = declare_encoding_syntax(spec->syntaxes[0], fd, spec->bits);
-        if (!result) goto wesrd_exit;
+        if (!result) goto exit;
     }
 
     dprintf(fd, "\n");
 
     /* Vérification que le décodage est possible */
 
-    result &= check_bits_correctness(spec->bits, fd);
-    if (!result) goto wesrd_exit;
+    result = check_bits_correctness(spec->bits, fd);
+    if (!result) goto exit;
 
     dprintf(fd, "\n");
 
@@ -406,15 +411,20 @@ bool write_encoding_spec_raw_disass(const encoding_spec *spec, int fd, const cha
     /* Définition des champs bruts */
 
     result = define_used_bits_fields(spec->bits, fd);
-    if (!result) goto wesrd_exit;
+    if (!result) goto exit;
 
     suffix = build_encoding_spec_prefix(spec);
+    if (suffix == NULL) goto exit;
+
     make_string_upper(suffix);
 
-    asprintf(&sid, "%s_%s", id, suffix);
+    ret = asprintf(&sid, "%s_%s", id, suffix);
 
     free(suffix);
 
+    if (ret == -1)
+        goto exit;
+
     for (i = 0; i < spec->syntax_count && result; i++)
     {
         if (spec->syntax_count > 1)
@@ -426,11 +436,14 @@ bool write_encoding_spec_raw_disass(const encoding_spec *spec, int fd, const cha
     free(sid);
 
     if (!result)
-        goto wesrd_exit;
+        goto exit;
 
     /* Encodage en dernier lieu */
 
-    asprintf(&encoding_fc, "g_%s_instruction_set_encoding", arch);
+    ret = asprintf(&encoding_fc, "g_%s_instruction_set_encoding", arch);
+
+    if (ret == -1)
+        goto exit;
 
     cast = build_cast_if_needed(encoding_fc);
 
@@ -463,7 +476,7 @@ bool write_encoding_spec_raw_disass(const encoding_spec *spec, int fd, const cha
 
     }
 
- wesrd_exit:
+ exit:
 
     return result;
 
@@ -581,9 +594,11 @@ bool write_encoding_spec_keywords(const encoding_spec *spec, int fd, const char
     asm_pattern *pattern;                   /* Définition d'assemblage     */
     const char *keyword;                    /* Mot clef principal          */
 
-    result = true;
-
     suffix = build_encoding_spec_prefix(spec);
+
+    result = (suffix != NULL);
+    if (!result) goto exit;
+
     make_string_upper(suffix);
 
     for (i = 0; i < spec->syntax_count; i++)
@@ -607,6 +622,8 @@ bool write_encoding_spec_keywords(const encoding_spec *spec, int fd, const char
 
     free(suffix);
 
+ exit:
+
     return result;
 
 }
@@ -634,9 +651,11 @@ bool write_encoding_spec_subid(const encoding_spec *spec, int fd, const char *na
     instr_id *subid;                        /* Sous-identifiant de syntaxe */
     unsigned int idval;                     /* Identifiant unique attribué */
 
-    result = true;
-
     suffix = build_encoding_spec_prefix(spec);
+
+    result = (suffix != NULL);
+    if (!result) goto exit;
+
     make_string_upper(suffix);
 
     for (i = 0; i < spec->syntax_count; i++)
@@ -653,6 +672,8 @@ bool write_encoding_spec_subid(const encoding_spec *spec, int fd, const char *na
 
     free(suffix);
 
+ exit:
+
     return result;
 
 }
@@ -687,6 +708,10 @@ bool write_encoding_spec_hooks(const encoding_spec *spec, int fd, const char *na
         if (refine)
         {
             suffix = build_encoding_spec_prefix(spec);
+
+            result = (suffix != NULL);
+            if (!result) goto exit;
+
             make_string_upper(suffix);
 
             for (i = 0; i < spec->syntax_count; i++)
@@ -730,6 +755,8 @@ bool write_encoding_spec_hooks(const encoding_spec *spec, int fd, const char *na
 
     }
 
+ exit:
+
     return result;
 
 }
diff --git a/tools/d2c/format/tokens.l b/tools/d2c/format/tokens.l
index 5db4afa..9bf42af 100644
--- a/tools/d2c/format/tokens.l
+++ b/tools/d2c/format/tokens.l
@@ -23,9 +23,15 @@
 
 .               {
                     char *msg;
-                    asprintf(&msg, "Unhandled token in d2c format block: '%s'", yytext);
-                    YY_FATAL_ERROR(msg);
-                    free(msg);
+                    int ret;
+                    ret = asprintf(&msg, "Unhandled token in d2c format block: '%s'", yytext);
+                    if (ret == -1)
+                        YY_FATAL_ERROR("Unhandled token in undisclosed d2c format block");
+                    else
+                    {
+                        YY_FATAL_ERROR(msg);
+                        free(msg);
+                    }
                 }
 
 
diff --git a/tools/d2c/hooks/tokens.l b/tools/d2c/hooks/tokens.l
index 1f72d2c..57a445b 100644
--- a/tools/d2c/hooks/tokens.l
+++ b/tools/d2c/hooks/tokens.l
@@ -23,9 +23,15 @@
 
 .                       {
                             char *msg;
-                            asprintf(&msg, "Unhandled token in d2c hooks block: '%s'", yytext);
-                            YY_FATAL_ERROR(msg);
-                            free(msg);
+                            int ret;
+                            ret = asprintf(&msg, "Unhandled token in d2c hooks block: '%s'", yytext);
+                            if (ret == -1)
+                                YY_FATAL_ERROR("Unhandled token in undisclosed d2c hooks block");
+                            else
+                            {
+                                YY_FATAL_ERROR(msg);
+                                free(msg);
+                            }
                         }
 
 
diff --git a/tools/d2c/id/tokens.l b/tools/d2c/id/tokens.l
index 24f18f1..73e4ce6 100644
--- a/tools/d2c/id/tokens.l
+++ b/tools/d2c/id/tokens.l
@@ -24,9 +24,15 @@
 
 .               {
                     char *msg;
-                    asprintf(&msg, "Unhandled token in d2c id block: '%s'", yytext);
-                    YY_FATAL_ERROR(msg);
-                    free(msg);
+                    int ret;
+                    ret = asprintf(&msg, "Unhandled token in d2c id block: '%s'", yytext);
+                    if (ret == -1)
+                        YY_FATAL_ERROR("Unhandled token in undisclosed d2c id block");
+                    else
+                    {
+                        YY_FATAL_ERROR(msg);
+                        free(msg);
+                    }
                 }
 
 
diff --git a/tools/d2c/pattern/tokens.l b/tools/d2c/pattern/tokens.l
index bef1e44..68dfca8 100644
--- a/tools/d2c/pattern/tokens.l
+++ b/tools/d2c/pattern/tokens.l
@@ -25,9 +25,15 @@
 
 .      {
            char *msg;
-           asprintf(&msg, "Unhandled token in d2c pattern block: '%s'", yytext);
-           YY_FATAL_ERROR(msg);
-           free(msg);
+           int ret;
+           ret = asprintf(&msg, "Unhandled token in d2c pattern block: '%s'", yytext);
+           if (ret == -1)
+               YY_FATAL_ERROR("Unhandled token in undisclosed d2c pattern block");
+           else
+           {
+               YY_FATAL_ERROR(msg);
+               free(msg);
+           }
        }
 
 
diff --git a/tools/d2c/rules/tokens.l b/tools/d2c/rules/tokens.l
index 6b14a85..7c6c020 100644
--- a/tools/d2c/rules/tokens.l
+++ b/tools/d2c/rules/tokens.l
@@ -55,9 +55,15 @@
 
 .                   {
                         char *msg;
-                        asprintf(&msg, "Unhandled token in d2c rules block: '%s'", yytext);
-                        YY_FATAL_ERROR(msg);
-                        free(msg);
+                        int ret;
+                        ret = asprintf(&msg, "Unhandled token in d2c rules block: '%s'", yytext);
+                        if (ret == -1)
+                            YY_FATAL_ERROR("Unhandled token in undisclosed d2c rules block");
+                        else
+                        {
+                            YY_FATAL_ERROR(msg);
+                            free(msg);
+                        }
                     }
 
 
diff --git a/tools/d2c/tokens.l b/tools/d2c/tokens.l
index 6d32e33..4bd90d6 100644
--- a/tools/d2c/tokens.l
+++ b/tools/d2c/tokens.l
@@ -119,10 +119,16 @@
 
 <*>.                            {
                                     char *msg;
-                                    asprintf(&msg, "Unhandled token in d2c definition: '%s'", yytext);
-                                    YY_FATAL_ERROR(msg);
-                                    free(msg);
-                                }
+                                    int ret;
+                                    ret = asprintf(&msg, "Unhandled token in d2c definition: '%s'", yytext);
+                                    if (ret == -1)
+                                        YY_FATAL_ERROR("Unhandled token in undisclosed d2c definition");
+                                    else
+                                    {
+                                        YY_FATAL_ERROR(msg);
+                                        free(msg);
+                                    }
+ }
 
 
 %%
-- 
cgit v0.11.2-87-g4458