Skip to content

Commit 9b5d63e

Browse files
committed
Make vMajor, vMinor, vPatch globals unsigned
This lets the compiler statically prove e.g. vPatch >= 0 is a constant so it can optimize away unnecessary comparisons after inlining and constant-propagating calls to versionAtOrAbove(). Signed-off-by: Cory Snider <[email protected]>
1 parent 2cf9b4c commit 9b5d63e

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

goopenssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ go_openssl_fips_enabled(void* handle)
6060
// and assign them to their corresponding function pointer
6161
// defined in goopenssl.h.
6262
void
63-
go_openssl_load_functions(void* handle, int major, int minor, int patch)
63+
go_openssl_load_functions(void* handle, unsigned int major, unsigned int minor, unsigned int patch)
6464
{
6565
#define DEFINEFUNC_INTERNAL(name, func) \
6666
_g_##name = dlsym(handle, func); \
6767
if (_g_##name == NULL) { \
68-
fprintf(stderr, "Cannot get required symbol " #func " from libcrypto version %d.%d\n", major, minor); \
68+
fprintf(stderr, "Cannot get required symbol " #func " from libcrypto version %u.%u\n", major, minor); \
6969
abort(); \
7070
}
7171
#define DEFINEFUNC(ret, func, args, argscall) \

goopenssl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int go_openssl_version_major(void* handle);
2424
int go_openssl_version_minor(void* handle);
2525
int go_openssl_version_patch(void* handle);
2626
int go_openssl_thread_setup(void);
27-
void go_openssl_load_functions(void* handle, int major, int minor, int patch);
27+
void go_openssl_load_functions(void* handle, unsigned int major, unsigned int minor, unsigned int patch);
2828
const GO_EVP_MD_PTR go_openssl_EVP_md5_sha1_backport(void);
2929

3030
// Define pointers to all the used OpenSSL functions.

init.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// as reported by the OpenSSL API.
1414
//
1515
// See Init() for details about file.
16-
func opensslInit(file string) (major, minor, patch int, err error) {
16+
func opensslInit(file string) (major, minor, patch uint, err error) {
1717
// Load the OpenSSL shared library using dlopen.
1818
handle, err := dlopen(file)
1919
if err != nil {
@@ -24,12 +24,13 @@ func opensslInit(file string) (major, minor, patch int, err error) {
2424
// Notice that major and minor could not match with the version parameter
2525
// in case the name of the shared library file differs from the OpenSSL
2626
// version it contains.
27-
major = int(C.go_openssl_version_major(handle))
28-
minor = int(C.go_openssl_version_minor(handle))
29-
patch = int(C.go_openssl_version_patch(handle))
30-
if major == -1 || minor == -1 || patch == -1 {
27+
imajor := int(C.go_openssl_version_major(handle))
28+
iminor := int(C.go_openssl_version_minor(handle))
29+
ipatch := int(C.go_openssl_version_patch(handle))
30+
if imajor < 0 || iminor < 0 || ipatch < 0 {
3131
return 0, 0, 0, errors.New("openssl: can't retrieve OpenSSL version")
3232
}
33+
major, minor, patch = uint(imajor), uint(iminor), uint(ipatch)
3334
var supported bool
3435
if major == 1 {
3536
supported = minor == 0 || minor == 1
@@ -43,7 +44,7 @@ func opensslInit(file string) (major, minor, patch int, err error) {
4344

4445
// Load the OpenSSL functions.
4546
// See shims.go for the complete list of supported functions.
46-
C.go_openssl_load_functions(handle, C.int(major), C.int(minor), C.int(patch))
47+
C.go_openssl_load_functions(handle, C.uint(major), C.uint(minor), C.uint(patch))
4748

4849
// Initialize OpenSSL.
4950
C.go_openssl_OPENSSL_init()

openssl.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
var (
2020
// vMajor and vMinor hold the major/minor OpenSSL version.
2121
// It is only populated if Init has been called.
22-
vMajor, vMinor, vPatch int
22+
vMajor, vMinor, vPatch uint
2323
)
2424

2525
var (
@@ -69,8 +69,12 @@ func Init(file string) error {
6969
return initErr
7070
}
7171

72+
func utoa(n uint) string {
73+
return strconv.FormatUint(uint64(n), 10)
74+
}
75+
7276
func errUnsupportedVersion() error {
73-
return errors.New("openssl: OpenSSL version: " + strconv.Itoa(vMajor) + "." + strconv.Itoa(vMinor) + "." + strconv.Itoa(vPatch))
77+
return errors.New("openssl: OpenSSL version: " + utoa(vMajor) + "." + utoa(vMinor) + "." + utoa(vPatch))
7478
}
7579

7680
type fail string
@@ -410,6 +414,6 @@ func CheckLeaks() {
410414
// versionAtOrAbove returns true when
411415
// (vMajor, vMinor, vPatch) >= (major, minor, patch),
412416
// compared lexicographically.
413-
func versionAtOrAbove(major, minor, patch int) bool {
417+
func versionAtOrAbove(major, minor, patch uint) bool {
414418
return vMajor > major || (vMajor == major && vMinor > minor) || (vMajor == major && vMinor == minor && vPatch >= patch)
415419
}

0 commit comments

Comments
 (0)