bug-libmatheval
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-libmatheval] segmentation fault using matheval


From: Aleksandar B. Samardzic
Subject: Re: [Bug-libmatheval] segmentation fault using matheval
Date: Thu, 29 Jul 2004 20:31:37 +0200
User-agent: Mutt/1.4.2.1i

On Thu, Jul 29, 2004 at 09:12:34AM +0200, Ferrarese Leopoldo wrote:
> Hi,
> I want use the libmatheval in a extension php under apache.
> In this way I can implement a simple interface to php.
> 
> the extension is very simple:
> the php give me a string and after I call a simple function:
> 
> int calcola(char* string, char* res)
> {
>   int length;       /* Length of above buffer. */
>   void *f;          /* Evaluators for function and function derivative.  */
>   double x=0;       /* Variable x value.  */
>   f = evaluator_create(string);
>   if (f==NULL)
>      {
>       res[0]='\0';
>       return 0;
>      }
>   y=evaluator_evaluate_x (f, x);
>   sprintf(res,"%f",y);
>   return 1;
> }
> 
> This function is very stupid, but sometime evaluator_create(string) fails!
> return NULL or the function create a segmentation fault!!!
> But if I call again (with same string obviously) it works fine.
> 
> why?
> 
> the lib is thread safe?

Thanks for the problem report.  There exist no global variables in
library so it should be fully thread-safe.  Could you please try
problematic functions with test program utilizing your function that I'm
attaching to this message and then let me know of input that will make
problem to appear?  Regarding segfaulting - I hope you allocated large
enough buffer for res argument (would suggest using snprintf() instead
of sprintf() anyway)?  Further, evaluator_create() will return NULL if
string passed to this function is invalid regarding allowed syntax (see
evaluator_create() node in library documentation for explanation what is
allowed).

Regards,
Alex


/***************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <matheval.h>

#define BUFFER_SIZE 80

int
calcola(char *string, char *res)
{
        int             length;
        void           *f;
        double          x = 0,
            y;

        f = evaluator_create(string);
        if (f == NULL) {
                res[0] = '\0';
                return 0;
        }

        y = evaluator_evaluate_x(f, x);
        sprintf(res, "%f", y);

        return 1;
}

int
main()
{
        char            string[BUFFER_SIZE],
                        result[BUFFER_SIZE];
        int             length;

        if (fgets(string, BUFFER_SIZE, stdin) == NULL)
                return EXIT_FAILURE;

        if ((length = strlen(string)) > 0 && string[length - 1] == '\n')
                string[length - 1] = '\0';

        if (calcola(string, result))
                printf("%s\n", result);

        return EXIT_SUCCESS;
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]