summaryrefslogtreecommitdiff
path: root/tools/d2c/coder.c
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2021-01-05 22:09:49 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2021-01-05 22:09:49 (GMT)
commit2a429fde26212e8c8c9a5a44f9a4a06ee60a5208 (patch)
treed5456f3c83d1faffdda1784b04d3ae59efde5ae0 /tools/d2c/coder.c
parente790ebee8ad78e91fc61738c5c40062ed36b1d44 (diff)
Fortified the d2c compiler by checking asprint() return values.
Diffstat (limited to 'tools/d2c/coder.c')
-rw-r--r--tools/d2c/coder.c208
1 files changed, 163 insertions, 45 deletions
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;