Hi!

>>>>> "ch" == ch  <[EMAIL PROTECTED]> writes:

>> Description:
ch>     My udf function and an equal udf function I found in the internet
ch>     seem to work well as does substring_index. Only when I call them
ch>     together I get a bug. As my function (quotet below) is so simple 
ch>     that it doubtly has a bug I guess substring_index has one.
>> How-To-Repeat:
mysql> select substring_index("1.2.3.4/24","/",1) ;
ch>     +-------------------------------------+
ch>     | substring_index("1.2.3.4/24","/",1) |
ch>     +-------------------------------------+
ch>     | 1.2.3.4                             |
ch>     +-------------------------------------+
ch>     1 row in set (0.00 sec)

mysql> select ewu_aton("1.2.3.4");                 
ch>     +---------------------+
ch>     | ewu_aton("1.2.3.4") |
ch>     +---------------------+
ch>     |            16909060 |
ch>     +---------------------+
ch>     1 row in set (0.01 sec)

mysql> select ewu_aton( substring_index("1.2.3.4/24","/",1) );
ch>     +-------------------------------------------------+
ch>     | ewu_aton( substring_index("1.2.3.4/24","/",1) ) |
ch>     +-------------------------------------------------+
ch>     |                                      3772388104 |
ch>     +-------------------------------------------------+
ch>     1 row in set (0.00 sec)
        
ch>     Here the correct value should had been 16909060 (0x01020304), too!


ch>     My Function:
ch>     /*
ch>     ** inet_aton() 
ch>     */
ch>     my_bool ewu_aton_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
ch>     {
ch>       if (args->arg_count != 1 || args->arg_type[0] != STRING_RESULT)
ch>       {
ch>         strmov(message,"Wrong arguments to ewu_aton;  Use the source!");
ch>         return 1;
ch>       }
initid-> max_length=20;
initid-> maybe_null=0;
ch>       return 0;
ch>     }
ch>     long long ewu_aton(UDF_INIT *initid, UDF_ARGS *args,
ch>                        char *is_null, char *error)
ch>     {
ch>       struct in_addr        in;
         
ch>       inet_aton(((char*) args->args[0]), &in);
ch>       *is_null=0;

ch>       return htonl(in.s_addr);
ch>     }

>> Fix:
ch>     I'd wish I'd have one :-)

The problem is probaly that you are assuming that the string argument
ends with a '\0', which may not be the case!

Try this instead:

        long long ewu_aton(UDF_INIT *initid, UDF_ARGS *args,
                           char *is_null, char *error)
        {
          struct in_addr        in;
          char buff[20];
          strnmov(buff,(char*) args->args[0], min(args->lengths[0],20));
         
          inet_aton(buff, &in);
          *is_null=0;

          return htonl(in.s_addr);
        }

Regards,
Monty

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to