From 47d229f3d9383ebcbde1b37e2a7afdfdbaed5f85 Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Thu, 17 Mar 2022 22:01:01 +0000 Subject: [PATCH] bugfix for numericx_translate() number argument * numericx_translate() was doing a reverse of the string argument, which it cannot. Now the reverse is using a malloced version of the string argument. * add free() to malloced number from number argument Signed-off-by: Daniel Santos --- numericx.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/numericx.c b/numericx.c index be20422..ccf6f4f 100644 --- a/numericx.c +++ b/numericx.c @@ -318,7 +318,7 @@ numeral_to_string(numeral_ptr* numeral, bool result_with_units_on_the_end) * @param to_units_on_the_end - does the translate 'to' numerical system have units on the end (on the right)? * @param to_first_number_void - does the translate 'to' numerical system start counting on the second number? * @param to_infinite_base - is the translate 'to' numerical system first numeral infinite? For example, if first numeral is 'A', then does 'A' == 'AA' == 'AAA' == 'AAAA' ... ? - * @param number - number of the 'from' numerical system, to be translated into the 'to' numerical system. + * @param number_arg - number of the 'from' numerical system, to be translated into the 'to' numerical system. * @param result_string - string to where to store the result. * * @return EINVAL if argument of result_string is not NULL. @@ -327,14 +327,14 @@ numeral_to_string(numeral_ptr* numeral, bool result_with_units_on_the_end) * @return EXIT_SUCCESS in case of success. */ int -numericx_translate(char* from, bool from_units_on_the_end, bool from_first_number_void, bool from_infinite_base, char* to, bool to_units_on_the_end, bool to_first_number_void, bool to_infinite_base, char* number, char** result_string) +numericx_translate(char* from, bool from_units_on_the_end, bool from_first_number_void, bool from_infinite_base, char* to, bool to_units_on_the_end, bool to_first_number_void, bool to_infinite_base, char* number_arg, char** result_string) { /* result_string has to be NULL */ if( !(*result_string == NULL) ) return EINVAL; /* Check if number belongs to it's numerical system */ - if( !is_valid_number(from, number) ) + if( !is_valid_number(from, number_arg) ) return EDOM; /* _first and _last variables */ @@ -344,6 +344,10 @@ numericx_translate(char* from, bool from_units_on_the_end, bool from_first_numbe char* to_first = to; char* to_last = to + (strlen(to) - 1); + /* number_arg to num for the maybe reverse */ + char* number = malloc((strlen(number_arg) + 1) * sizeof(char)); + strcpy(number, number_arg); + if( from_units_on_the_end ) { reverse_string(number); @@ -369,6 +373,7 @@ numericx_translate(char* from, bool from_units_on_the_end, bool from_first_numbe { free_numeral(counting); free_numeral(result); + free(number); return ERANGE; } @@ -402,6 +407,7 @@ numericx_translate(char* from, bool from_units_on_the_end, bool from_first_numbe /* free memory */ free_numeral(counting); free_numeral(result); + free(number); return EXIT_SUCCESS; }