Debian Codegraph @

compiler-rt/test/sanitizer_common/TestCases/Linux/getservent_r.cpp

raw
1 // RUN: %clangxx -O0 %s -o %t && %run %t
2
3 // REQUIRES: glibc, netbase
4
5 #include <arpa/inet.h>
6 #include <assert.h>
7 #include <fcntl.h>
8 #include <netdb.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 void CheckResult(const char *file, int line, int ret) {
15 if (ret != 0) {
16 fprintf(stderr, "ERROR: %s:%d - %s\n", file, line, strerror(ret));
17 }
18 assert(ret == 0);
19 }
20
21 #define CHECK_RESULT(ret) CheckResult(__FILE__, __LINE__, ret)
22
23 int main(void) {
24 assert(access("/etc/services", O_RDONLY) == 0);
25 struct servent result_buf;
26 struct servent *result;
27 char buf[1024];
28 // If these fail, check /etc/services if "ssh" exists. I picked this because
29 // it should exist everywhere, if it doesn't, I am sorry. Disable the test
30 // then please.
31 CHECK_RESULT(
32 getservbyname_r("ssh", nullptr, &result_buf, buf, sizeof(buf), &result));
33 assert(result != nullptr);
34 CHECK_RESULT(getservbyport_r(htons(22), nullptr, &result_buf, buf,
35 sizeof(buf), &result));
36 assert(result != nullptr);
37
38 CHECK_RESULT(getservent_r(&result_buf, buf, sizeof(buf), &result));
39 assert(result != nullptr);
40
41 CHECK_RESULT(getservbyname_r("invalidhadfuiasdhi", nullptr, &result_buf, buf,
42 sizeof(buf), &result));
43 assert(result == nullptr);
44 }