Skip to content

Commit

Permalink
openvino: Fix OpenvinoVersion dangling pointer
Browse files Browse the repository at this point in the history
Fixed dangling pointer in OpenVinoVersion by copying the version
string into a malloc'd buffer and adding a corresponding 'defer C.free'
call to inference_engine.go.

Also updated the test to ensure the version string length is sensible.
A warning is printed if '2022.1' is not included in the version string.
  • Loading branch information
deradiri authored and deadprogram committed Jul 20, 2022
1 parent bd1426a commit 23383f6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
13 changes: 10 additions & 3 deletions openvino/ie/inference_engine.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#include "inference_engine.h"

const char* OpenVinoVersion() {
std::ostringstream res;
res << std::to_string(InferenceEngine::GetInferenceEngineVersion()->apiVersion.major)
std::ostringstream buf;
buf << std::to_string(InferenceEngine::GetInferenceEngineVersion()->apiVersion.major)
<< "."
<< std::to_string(InferenceEngine::GetInferenceEngineVersion()->apiVersion.minor)
<< "."
<< InferenceEngine::GetInferenceEngineVersion()->buildNumber;
return res.str().c_str();
auto version = buf.str();

size_t resLen = version.size() + 1;
auto res = (char*)malloc(resLen);

memset(res ,0, resLen);
memcpy(res, version.c_str(), resLen);
return res;
}
5 changes: 4 additions & 1 deletion openvino/ie/inference_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ package ie
import (
"C"
)
import "unsafe"

// Version returns the current Inference Engine library version
func Version() string {
return C.GoString(C.OpenVinoVersion())
v := C.OpenVinoVersion()
defer C.free(unsafe.Pointer(v))
return C.GoString(v)
}
12 changes: 11 additions & 1 deletion openvino/ie/inference_engine_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package ie

import (
"strings"
"testing"
)

func TestInferenceEngineVersion(t *testing.T) {
if Version() == "" {
v := Version()
expected := "2022.1"
if v == "" || len(v) < len(expected) {
t.Error("Invalid IE Version")
return
}

// Different platforms can have different strings, only warn if expected
// version string does not match.
// eg. archlinux: 2.1.custom_makepkg_cdb9bec7210f8c24fde3e416c7ada820faaaa23e
if !strings.Contains(v, expected) {
t.Log("Unexpected IE Version: ", v)
}
}

0 comments on commit 23383f6

Please sign in to comment.