Skip to content

Commit

Permalink
Make vMajor, vMinor, vPatch globals unsigned
Browse files Browse the repository at this point in the history
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 <csnider@mirantis.com>
  • Loading branch information
corhere committed Oct 18, 2023
1 parent 8723005 commit eb456ec
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions goopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ go_openssl_fips_enabled(void* handle)
// and assign them to their corresponding function pointer
// defined in goopenssl.h.
void
go_openssl_load_functions(void* handle, int major, int minor, int patch)
go_openssl_load_functions(void* handle, unsigned int major, unsigned int minor, unsigned int patch)
{
#define DEFINEFUNC_INTERNAL(name, func) \
_g_##name = dlsym(handle, func); \
if (_g_##name == NULL) { \
fprintf(stderr, "Cannot get required symbol " #func " from libcrypto version %d.%d\n", major, minor); \
fprintf(stderr, "Cannot get required symbol " #func " from libcrypto version %u.%u\n", major, minor); \
abort(); \
}
#define DEFINEFUNC(ret, func, args, argscall) \
Expand Down
2 changes: 1 addition & 1 deletion goopenssl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int go_openssl_version_major(void* handle);
int go_openssl_version_minor(void* handle);
int go_openssl_version_patch(void* handle);
int go_openssl_thread_setup(void);
void go_openssl_load_functions(void* handle, int major, int minor, int patch);
void go_openssl_load_functions(void* handle, unsigned int major, unsigned int minor, unsigned int patch);
const GO_EVP_MD_PTR go_openssl_EVP_md5_sha1_backport(void);

// Define pointers to all the used OpenSSL functions.
Expand Down
13 changes: 7 additions & 6 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// as reported by the OpenSSL API.
//
// See Init() for details about file.
func opensslInit(file string) (major, minor, patch int, err error) {
func opensslInit(file string) (major, minor, patch uint, err error) {
// Load the OpenSSL shared library using dlopen.
handle, err := dlopen(file)
if err != nil {
Expand All @@ -24,12 +24,13 @@ func opensslInit(file string) (major, minor, patch int, err error) {
// Notice that major and minor could not match with the version parameter
// in case the name of the shared library file differs from the OpenSSL
// version it contains.
major = int(C.go_openssl_version_major(handle))
minor = int(C.go_openssl_version_minor(handle))
patch = int(C.go_openssl_version_patch(handle))
if major == -1 || minor == -1 || patch == -1 {
imajor := int(C.go_openssl_version_major(handle))
iminor := int(C.go_openssl_version_minor(handle))
ipatch := int(C.go_openssl_version_patch(handle))
if imajor < 0 || iminor < 0 || ipatch < 0 {
return 0, 0, 0, errors.New("openssl: can't retrieve OpenSSL version")
}
major, minor, patch = uint(imajor), uint(iminor), uint(ipatch)
var supported bool
if major == 1 {
supported = minor == 0 || minor == 1
Expand All @@ -43,7 +44,7 @@ func opensslInit(file string) (major, minor, patch int, err error) {

// Load the OpenSSL functions.
// See shims.go for the complete list of supported functions.
C.go_openssl_load_functions(handle, C.int(major), C.int(minor), C.int(patch))
C.go_openssl_load_functions(handle, C.uint(major), C.uint(minor), C.uint(patch))

// Initialize OpenSSL.
C.go_openssl_OPENSSL_init()
Expand Down
10 changes: 7 additions & 3 deletions openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
var (
// vMajor and vMinor hold the major/minor OpenSSL version.
// It is only populated if Init has been called.
vMajor, vMinor, vPatch int
vMajor, vMinor, vPatch uint
)

var (
Expand Down Expand Up @@ -69,8 +69,12 @@ func Init(file string) error {
return initErr
}

func utoa(n uint) string {
return strconv.FormatUint(uint64(n), 10)
}

func errUnsupportedVersion() error {
return errors.New("openssl: OpenSSL version: " + strconv.Itoa(vMajor) + "." + strconv.Itoa(vMinor) + "." + strconv.Itoa(vPatch))
return errors.New("openssl: OpenSSL version: " + utoa(vMajor) + "." + utoa(vMinor) + "." + utoa(vPatch))
}

type fail string
Expand Down Expand Up @@ -410,6 +414,6 @@ func CheckLeaks() {
// versionAtOrAbove returns true when
// (vMajor, vMinor, vPatch) >= (major, minor, patch),
// compared lexicographically.
func versionAtOrAbove(major, minor, patch int) bool {
func versionAtOrAbove(major, minor, patch uint) bool {
return vMajor > major || (vMajor == major && vMinor >= minor) || (vMajor == major && vMinor == minor && vPatch >= patch)
}

0 comments on commit eb456ec

Please sign in to comment.