openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [cpp-ue4] API often tries to set reference to null when calling GetHttpRetryManager

Open leith-bartrich opened this issue 5 years ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Currently, development HEAD generates code that copies a pointer to a reference without doing a null check. The pointer in question is initialized to nullptr and isn't set to anything else until other subsequent code may or may not set it to an instance. There is no documentation to suggest it is somehow illegal to call GetHttpRetryManager before other logic is run.

in cpp, setting a reference to nullptr is undefined behavior. In my case, it generates an access violation when one later tries to read the results of the given getter function.

There are other issues with how the API object is handling the HttpRetryManager. However, this is a simple bug to suggest that there should be a null check, and then probably a UE_LOG of type Fatal should be thrown. So at least, a developer who calls this method before the field is set, gets some feedback rather than undefined cpp behavior occurring later in their own code.

https://docs.unrealengine.com/4.26/en-US/API/Runtime/Core/Logging/ELogVerbosity__Type/

I'll author and submit a pull request shortly.

sidenote: there's what looks to be a bug in SetHttpRetryManager where it's comparing the field to the getter, when probably it means to check the method parameter against the field? It's hard to overlook it when looking at this. But really this bug report and fix will be limited to the undefined behavior of getting the reference.

openapi-generator version

development HEAD

OpenAPI declaration file content or url

any valid API would generate this code.

Generation Details

any valid API would generate this code.

Steps to reproduce

generate any valid api schema

Related issues/PRs
Suggest a fix

leith-bartrich avatar Aug 21 '21 18:08 leith-bartrich

I should probably show the generated code at issue eh?

void OpenAPIOrgbuilderApi::SetHttpRetryManager(FHttpRetrySystem::FManager& InRetryManager)
{
	if(RetryManager != &GetHttpRetryManager())
	{
		DefaultRetryManager.Reset();
		RetryManager = &InRetryManager;
	}
}

FHttpRetrySystem::FManager& OpenAPIOrgbuilderApi::GetHttpRetryManager()
{
	return *RetryManager;
}

The getter dereferences the pointer directly to the return type which is a reference, without doing a null check. So far, in my codebases, the field is actually always null. And I may be stumbling onto subsequent bugs as well.

leith-bartrich avatar Aug 21 '21 18:08 leith-bartrich