mesos: Segfault in net::getIP

Time: 六月 14, 2015
Category: mesos

今天把mesos升级到0.22.1之后slave一起来就core了,打开debug调试了一下:

Program terminated with signal 11, Segmentation fault.
#0 0x00007f639867c77e in free () from /lib64/libc.so.6
(gdb) bt
#0 0x00007f639867c77e in free () from /lib64/libc.so.6
#1 0x00007f63986c25d0 in freeaddrinfo () from /lib64/libc.so.6
#2 0x00007f6399deeafa in net::getIP (hostname="<redacted>", family=2)
   at ./3rdparty/stout/include/stout/net.hpp:201
#3 0x00007f6399e1f273 in process::initialize (delegate=Unhandled dwarf
   expression opcode 0xf3) at src/process.cpp:837
#4 0x000000000042342f in main ()

其中net::getIP()函数的实现如下:

// Returns a Try of the IP for the provided hostname or an error if no
// IP is obtained.
inline TrygetIP(const std::string& hostname, sa_family_t family)
{
  struct addrinfo hints, *result;
  hints = createAddrInfo(SOCK_STREAM, family, 0);

  int error = getaddrinfo(hostname.c_str(), NULL, &hints, &result);
  if (error != 0 || result == NULL) {
    if (result != NULL ) {
      freeaddrinfo(result);
    }
    return Error(gai_strerror(error));
  }
  if (result->ai_addr == NULL) {
    freeaddrinfo(result);
    return Error("Got no addresses for '" + hostname + "'");
  }

  uint32_t ip = ((struct sockaddr_in*)(result->ai_addr))->sin_addr.s_addr;
  freeaddrinfo(result);

  return ip;
}

result没有初始化,并且在getaddrinfo()函数失败后仍然检查,这里明显是有问题的。因为如果getaddrinfo()失败,result的行为是未定义的。

目前这个bug已经在主干上修复了:

https://issues.apache.org/jira/browse/MESOS-2636?jql=text%20~%20%22getIP%22

Leave a Comment