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 <dacs.git@brilhante.top>
This commit is contained in:
Daniel Santos 2022-03-17 22:01:01 +00:00
parent 63a4b2b9bb
commit 47d229f3d9
1 changed files with 9 additions and 3 deletions

View File

@ -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;
}