Bug #1200 Can't start MySQL if bind-address set to hostname that starts with a number
Submitted: 4 Sep 2003 17:44 Modified: 11 Sep 2003 3:19
Reporter: Gary Lewis Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:3.23.55 OS:Windows (Windows XP)
Assigned to: Ramil Kalimullin CPU Architecture:Any

[4 Sep 2003 17:44] Gary Lewis
Description:
On a machine named "8C3X801" MySQL fails to start if the my.ini bind-address parameter is set to this hostname. 

My.ini snippet:

[mysqld]
bind-address=8C3X801

Setting the bind-address to the IP of the machine allowed MySQL to start normally.

How to repeat:

Simply create a machine with a hostname that starts with a digit and set the bind-address to that hostname and try to start MySQL.

Suggested fix:

I have checked the MySQL source code (I found the same code in 4.0.14 and 3.23.55) and found the following code in the get_options() function in mysqld.cpp at line 3899:

    case (int) OPT_BIND_ADDRESS:
       if (optarg && isdigit(optarg[0]))
       {
          my_bind_addr = (ulong) inet_addr(optarg);
       } else
       {
          struct hostent *ent;
          if (optarg && optarg[0])
             ent=gethostbyname(optarg);
          else
          {
             char myhostname[255];
             if (gethostname(myhostname,sizeof(myhostname)) < 0)
             {
                sql_perror("Can't start server: cannot get my own hostname!");
                exit(1);
             }
             ent=gethostbyname(myhostname);
          }
          if (!ent)
          {
             sql_perror("Can't start server: cannot resolve hostname!");
             exit(1);
          }
          my_bind_addr = (ulong) ((in_addr*)ent->h_addr_list[0])->s_addr;
       }
      break;

The call to isdigit(optarg[0]) will return true since the hostname starts with a digit making MySQL think the bind-address option specifies an IP address. This will eventually cause the server to be unable to bind() to this address.

Instead of using isdigit(optarg[0]) to test for IP/hostname I suggest calling inet_addr(optarg). If this call returns INADDR_NONE then it is a hostname, otherwise it is an IP address:

my_bind_addr = (ulong) inet_addr(optarg);
if (INADDR_NONE == my_bind_addr)
{
  // Code to resolve hostname
}
[11 Sep 2003 3:19] Ramil Kalimullin
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html

Tnaks a lot for so good bug report!
The issue has been fixed in 4.1 source tree.