Added feature to return error while parsing c header file (#13030)

* Added feature to return error while parsing c header file

* Fixed cmd_type.c to pass tests
This commit is contained in:
Gaurav Kumar Ghildiyal 2019-02-10 06:26:28 +05:30 committed by radare
parent 54512ee776
commit e60749ed12
4 changed files with 42 additions and 8 deletions

View File

@ -741,7 +741,7 @@ R_API int r_core_run_script(RCore *core, const char *file) {
free (out);
}
} else if (r_parse_is_c_file (file)) {
char *out = r_parse_c_file (core->anal, file);
char *out = r_parse_c_file (core->anal, file, NULL);
if (out) {
r_cons_strcat (out);
sdb_query_lines (core->anal->sdb_types, out);

View File

@ -1027,21 +1027,31 @@ static int cmd_type(void *data, const char *input) {
if (!strcmp (filename, "-")) {
char *tmp = r_core_editor (core, "*.h", "");
if (tmp) {
char *out = r_parse_c_string (core->anal, tmp);
char *error_msg = NULL;
char *out = r_parse_c_string (core->anal, tmp, &error_msg);
if (out) {
// r_cons_strcat (out);
save_parsed_type (core, out);
free (out);
}
if (error_msg) {
fprintf (stderr, "%s", error_msg);
free (error_msg);
}
free (tmp);
}
} else {
char *out = r_parse_c_file (core->anal, filename);
char *error_msg = NULL;
char *out = r_parse_c_file (core->anal, filename, &error_msg);
if (out) {
//r_cons_strcat (out);
save_parsed_type (core, out);
free (out);
}
if (error_msg) {
fprintf (stderr, "%s", error_msg);
free (error_msg);
}
}
free (homefile);
} else if (input[1] == 's') {
@ -1069,11 +1079,16 @@ static int cmd_type(void *data, const char *input) {
} else if (input[1] == ' ') {
char tmp[8192];
snprintf (tmp, sizeof (tmp) - 1, "%s;", input + 2);
char *out = r_parse_c_string (core->anal, tmp);
char *error_msg = NULL;
char *out = r_parse_c_string (core->anal, tmp, &error_msg);
if (out) {
save_parsed_type (core, out);
free (out);
}
if (error_msg) {
fprintf (stderr, "%s", error_msg);
free (error_msg);
}
} else {
eprintf ("Invalid use of td. See td? for help\n");
}

View File

@ -61,8 +61,8 @@ R_API int r_parse_parse(RParse *p, const char *data, char *str);
R_API int r_parse_assemble(RParse *p, char *data, char *str);
R_API int r_parse_filter(RParse *p, ut64 addr, RFlag *f, char *data, char *str, int len, bool big_endian);
R_API bool r_parse_varsub(RParse *p, RAnalFunction *f, ut64 addr, int oplen, char *data, char *str, int len);
R_API char *r_parse_c_string(RAnal *anal, const char *code);
R_API char *r_parse_c_file(RAnal *anal, const char *path);
R_API char *r_parse_c_string(RAnal *anal, const char *code, char **error_msg);
R_API char *r_parse_c_file(RAnal *anal, const char *path, char **error_msg);
R_API int r_parse_is_c_file(const char *file);
R_API char *r_parse_immtrim(char *opstr);
R_API void r_parse_reset(void);

View File

@ -75,13 +75,31 @@ static int typeload(void *p, const char *k, const char *v) {
return 0;
}
R_API char *r_parse_c_file(RAnal *anal, const char *path) {
static void error_func(void *opaque, const char *msg) {
appendstring (msg, opaque);
char **p = (char **)opaque;
if (p && *p) {
int n = strlen(*p);
char *ptr = malloc (n + 2);
if (!ptr) {
return;
}
strcpy (ptr, *p);
ptr[n] = '\n';
ptr[n + 1] = 0;
free (*p);
*p = ptr;
}
}
R_API char *r_parse_c_file(RAnal *anal, const char *path, char **error_msg) {
char *str = NULL;
TCCState *T = tcc_new (anal->cpu, anal->bits, anal->os);
if (!T) {
return NULL;
}
tcc_set_callback (T, &appendstring, &str);
tcc_set_error_func (T, (void *)error_msg, error_func);
sdb_foreach (anal->sdb_types, typeload, anal);
if (tcc_add_file (T, path) == -1) {
free (str);
@ -91,13 +109,14 @@ R_API char *r_parse_c_file(RAnal *anal, const char *path) {
return str;
}
R_API char *r_parse_c_string(RAnal *anal, const char *code) {
R_API char *r_parse_c_string(RAnal *anal, const char *code, char **error_msg) {
char *str = NULL;
TCCState *T = tcc_new (anal->cpu, anal->bits, anal->os);
if (!T) {
return NULL;
}
tcc_set_callback (T, &appendstring, &str);
tcc_set_error_func (T, (void *)error_msg, error_func);
sdb_foreach (anal->sdb_types, typeload, NULL);
if (tcc_compile_string (T, code) != 0) {
free (str);