Skip to content

Commit a3eb2d1

Browse files
0rteipanacrolix
authored andcommitted
fix: make utp_types.h safer and compatible with cgo/Go builds\n\n- include <stdint.h> and <stdbool.h> for consistent integer/boolean types\n- avoid retypedefs when types already exist\n- normalize macros and spacing\n- keep original behavior on non-C99 platforms\n
1 parent 7ba422f commit a3eb2d1

1 file changed

Lines changed: 82 additions & 55 deletions

File tree

utp_types.h

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -26,83 +26,114 @@
2626
// Allow libutp consumers or prerequisites to override PACKED_ATTRIBUTE
2727
#ifndef PACKED_ATTRIBUTE
2828
#if defined BROKEN_GCC_STRUCTURE_PACKING && defined __GNUC__
29-
// Used for gcc tool chains accepting but not supporting pragma pack
30-
// See http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
31-
#define PACKED_ATTRIBUTE __attribute__((__packed__))
29+
// Used for gcc tool chains accepting but not supporting pragma pack
30+
// See http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
31+
#define PACKED_ATTRIBUTE __attribute__((__packed__))
3232
#else
33-
#define PACKED_ATTRIBUTE
33+
#define PACKED_ATTRIBUTE
3434
#endif // defined BROKEN_GCC_STRUCTURE_PACKING && defined __GNUC__
3535
#endif // ndef PACKED_ATTRIBUTE
3636

3737
#ifdef __GNUC__
38-
#define ALIGNED_ATTRIBUTE(x) __attribute__((aligned (x)))
38+
#define ALIGNED_ATTRIBUTE(x) __attribute__((aligned(x)))
3939
#else
40-
#define ALIGNED_ATTRIBUTE(x)
40+
#define ALIGNED_ATTRIBUTE(x)
4141
#endif
4242

4343
// hash.cpp needs socket definitions, which is why this networking specific
4444
// code is inclued in utypes.h
4545
#ifdef WIN32
46-
#define _CRT_SECURE_NO_DEPRECATE
47-
#define WIN32_LEAN_AND_MEAN
48-
#include <windows.h>
49-
#include <winsock2.h>
50-
#include <ws2tcpip.h>
51-
#define IP_OPT_DONTFRAG IP_DONTFRAGMENT
52-
#define SHUT_RD SD_RECEIVE
53-
#define SHUT_WR SD_SEND
54-
#define SHUT_RDWR SD_BOTH
46+
#define _CRT_SECURE_NO_DEPRECATE
47+
#define WIN32_LEAN_AND_MEAN
48+
#include <windows.h>
49+
#include <winsock2.h>
50+
#include <ws2tcpip.h>
51+
#define IP_OPT_DONTFRAG IP_DONTFRAGMENT
52+
#define SHUT_RD SD_RECEIVE
53+
#define SHUT_WR SD_SEND
54+
#define SHUT_RDWR SD_BOTH
5555
#else
56-
#include <netinet/in.h>
57-
#include <arpa/inet.h>
58-
#include <unistd.h>
59-
#include <sys/socket.h>
60-
61-
#ifdef IP_DONTFRAG
62-
#define IP_OPT_DONTFRAG IP_DONTFRAG
63-
#elif defined IP_DONTFRAGMENT
64-
#define IP_OPT_DONTFRAG IP_DONTFRAGMENT
65-
#else
66-
//#warning "I don't know how to set DF bit on this system"
67-
#endif
56+
#include <netinet/in.h>
57+
#include <arpa/inet.h>
58+
#include <unistd.h>
59+
#include <sys/socket.h>
60+
61+
#ifdef IP_DONTFRAG
62+
#define IP_OPT_DONTFRAG IP_DONTFRAG
63+
#elif defined IP_DONTFRAGMENT
64+
#define IP_OPT_DONTFRAG IP_DONTFRAGMENT
65+
#else
66+
// #warning "I don't know how to set DF bit on this system"
67+
#endif
6868
#endif
6969

7070
#ifdef _MSC_VER
71-
#include <BaseTsd.h>
72-
typedef SSIZE_T ssize_t;
71+
#include <BaseTsd.h>
72+
typedef SSIZE_T ssize_t;
7373
#endif
7474

7575
#ifdef POSIX
76-
typedef struct sockaddr_storage SOCKADDR_STORAGE;
76+
typedef struct sockaddr_storage SOCKADDR_STORAGE;
7777
#endif
7878

7979
#ifdef WIN32
80-
#define I64u "%I64u"
80+
#define I64u "%I64u"
8181
#else
82-
#define I64u "%Lu"
82+
#define I64u "%Lu"
8383
#endif
8484

85-
// standard types
85+
// Use standard integer and boolean types where possible.
86+
#include <stdint.h>
87+
88+
// Prefer stdbool for boolean type in C; C++ has built-in bool.
89+
#if !defined(__cplusplus)
90+
#include <stdbool.h>
91+
#endif
92+
93+
// standard types: define short aliases only if not already provided.
94+
#ifndef byte
8695
typedef unsigned char byte;
87-
typedef unsigned char uint8;
88-
typedef signed char int8;
89-
typedef unsigned short uint16;
90-
typedef signed short int16;
96+
#endif
97+
98+
#ifndef uint8
99+
typedef uint8_t uint8;
100+
#endif
101+
102+
#ifndef int8
103+
typedef int8_t int8;
104+
#endif
105+
106+
#ifndef uint16
107+
typedef uint16_t uint16;
108+
#endif
109+
110+
#ifndef int16
111+
typedef int16_t int16;
112+
#endif
113+
114+
#ifndef uint
91115
typedef unsigned int uint;
92-
typedef unsigned int uint32;
93-
typedef signed int int32;
116+
#endif
94117

95-
#ifdef _MSC_VER
96-
typedef unsigned __int64 uint64;
97-
typedef signed __int64 int64;
98-
#else
99-
typedef unsigned long long uint64;
100-
typedef long long int64;
118+
#ifndef uint32
119+
typedef uint32_t uint32;
120+
#endif
121+
122+
#ifndef int32
123+
typedef int32_t int32;
124+
#endif
125+
126+
#ifndef uint64
127+
typedef uint64_t uint64;
128+
#endif
129+
130+
#ifndef int64
131+
typedef int64_t int64;
101132
#endif
102133

103134
/* compile-time assert */
104135
#ifndef CASSERT
105-
#define CASSERT( exp, name ) typedef int is_not_##name [ (exp ) ? 1 : -1 ];
136+
#define CASSERT(exp, name) typedef int is_not_##name[(exp) ? 1 : -1];
106137
#endif
107138

108139
CASSERT(8 == sizeof(uint64), sizeof_uint64_is_8)
@@ -113,15 +144,11 @@ CASSERT(8 == sizeof(int64), sizeof_int64_is_8)
113144
#endif
114145

115146
// always ANSI
116-
typedef const char * cstr;
117-
typedef char * str;
147+
typedef const char *cstr;
148+
typedef char *str;
118149

119-
#ifndef __cplusplus
120-
#if defined __STDC_VERSION__ && __STDC_VERSION__ > 201710L
121-
/* bool, true and false are keywords. */
122-
#else
123-
typedef uint8 bool;
124-
#endif
125-
#endif
150+
/* No explicit typedef for `bool` here: we include <stdbool.h> above for C
151+
and rely on built-in `bool` in C++. Guarding typedefs for `bool` would
152+
` clash with platforms that already provide it. */
126153

127154
#endif //__UTP_TYPES_H__

0 commit comments

Comments
 (0)