aboutsummaryrefslogtreecommitdiff
path: root/libtcc.c
diff options
context:
space:
mode:
authorThomas Preud'homme <thomas.preudhomme@celest.fr>2011-07-08 10:51:26 +0200
committerThomas Preud'homme <thomas.preudhomme@celest.fr>2011-07-08 10:51:26 +0200
commit13121e220cb2b8ddbe1b4218b1734843493a95c8 (patch)
treeaa7b991dc87f2b1044ecdada3ffc068053914113 /libtcc.c
parentd01f65ef935d4d9eb78d4b9442a7538346b1f0a9 (diff)
downloadtinycc-13121e220cb2b8ddbe1b4218b1734843493a95c8.tar.gz
tinycc-13121e220cb2b8ddbe1b4218b1734843493a95c8.tar.bz2
Fix problem spotted in <4E15F966.4090102@gmx.de>
* Rename tcc_split_path_components * Move tcc_split_path below memory wrapper section * Ident tcc_split_path by 4 * Remove prefix and suffix clutter in tcc_split_path * Don't dereference beyond the end of the search paths string
Diffstat (limited to 'libtcc.c')
-rw-r--r--libtcc.c68
1 files changed, 33 insertions, 35 deletions
diff --git a/libtcc.c b/libtcc.c
index f060476..fedd7e5 100644
--- a/libtcc.c
+++ b/libtcc.c
@@ -247,34 +247,6 @@ PUB_FUNC char *tcc_strdup(const char *str)
return ptr;
}
-/* out must not point to a valid dynarray since a new one is created */
-PUB_FUNC int tcc_split_path_components(const char *in,
- const char * const *prefixs,
- int nb_prefixs, char ***out)
-{
- int i, nb_components = 0;
- char *path_component;
- const char *end_component;
- size_t path_size;
-
- *out = NULL;
- end_component = in;
- do {
- while (*end_component && *end_component != ':')
- ++end_component;
- for (i = 0; i < nb_prefixs; i++) {
- path_size = (strlen(prefixs[i]) + 1) * sizeof(char)
- + (end_component - in);
- path_component = tcc_malloc(path_size);
- pstrcpy(path_component, path_size, prefixs[i]);
- pstrcat(path_component, path_size, in);
- dynarray_add((void ***) out, &nb_components, path_component);
- }
- in = ++end_component;
- } while (*end_component);
- return nb_components;
-}
-
PUB_FUNC void tcc_memstats(void)
{
#ifdef MEM_DEBUG
@@ -321,6 +293,33 @@ PUB_FUNC void dynarray_reset(void *pp, int *n)
*(void**)pp = NULL;
}
+/* out must not point to a valid dynarray since a new one is created */
+PUB_FUNC int tcc_split_path(const char *in, const char * const *prefixs,
+ int nb_prefixs, char ***out)
+{
+ int i, nb_comps = 0;
+ char *path;
+ const char *end;
+ size_t size;
+
+ *out = NULL;
+ do {
+ end = in;
+ while (*end && *end != ':')
+ ++end;
+ for (i = 0; i < nb_prefixs; i++) {
+ size = (strlen(prefixs[i]) + 1) * sizeof(char)
+ + (end - in);
+ path = tcc_malloc(size);
+ pstrcpy(path, size, prefixs[i]);
+ pstrcat(path, size, in);
+ dynarray_add((void ***) out, &nb_comps, path);
+ }
+ in = end + 1;
+ } while (*end);
+ return nb_comps;
+}
+
/* we use our own 'finite' function to avoid potential problems with
non standard math libs */
/* XXX: endianness dependent */
@@ -1009,9 +1008,9 @@ LIBTCCAPI TCCState *tcc_new(void)
const char * const lddir_prefixs[] = {lddir_prefix1, lddir_prefix2};
nb_prefixs = sizeof lddir_prefixs / sizeof *lddir_prefixs;
- nb_extra_lddirs = tcc_split_path_components(CONFIG_TCC_EXTRA_LDDIR,
- lddir_prefixs, nb_prefixs,
- &extra_lddirs);
+ nb_extra_lddirs = tcc_split_path(CONFIG_TCC_EXTRA_LDDIR,
+ lddir_prefixs, nb_prefixs,
+ &extra_lddirs);
for (i = 0; i < nb_extra_lddirs; i++)
tcc_add_library_path(s, extra_lddirs[i]);
dynarray_reset(&extra_lddirs, &nb_extra_lddirs);
@@ -1337,10 +1336,9 @@ LIBTCCAPI int tcc_set_output_type(TCCState *s, int output_type)
incdir_prefix2};
nb_prefixs = sizeof incdir_prefixs / sizeof *incdir_prefixs;
- nb_extra_incdirs = tcc_split_path_components(CONFIG_TCC_INCSUBDIR,
- incdir_prefixs,
- nb_prefixs,
- &extra_incdirs);
+ nb_extra_incdirs = tcc_split_path(CONFIG_TCC_INCSUBDIR,
+ incdir_prefixs, nb_prefixs,
+ &extra_incdirs);
for (i = 0; i < nb_extra_incdirs; i++)
tcc_add_sysinclude_path(s, extra_incdirs[i]);
dynarray_reset(&extra_incdirs, &nb_extra_incdirs);