Skip to content

Instantly share code, notes, and snippets.

@omalinov
Created June 12, 2018 11:26
Show Gist options
  • Save omalinov/7af1edba9b552a64ede219409f6b8bc2 to your computer and use it in GitHub Desktop.
Save omalinov/7af1edba9b552a64ede219409f6b8bc2 to your computer and use it in GitHub Desktop.
test.cpp
void Utils::Trace(const char* fmt, ...)
{
FCM::AutoPtr<FCM::IFCMUnknown> pUnk;
FCM::AutoPtr<Application::Service::IOutputConsoleService> outputConsoleService;
FCM::Result tempRes = s_Callback->GetService(Application::Service::FLASHAPP_OUTPUT_CONSOLE_SERVICE, pUnk.m_Ptr);
outputConsoleService = pUnk;
pUnk.Reset();
/*char result[1024];
std::strcpy(result, fmt);
std::strcat(result, "\n");*/
if (outputConsoleService)
{
va_list args;
char buffer[1024];
va_start(args, fmt);
vsnprintf(buffer, 1024, fmt, args);
va_end(args);
std::strcat(buffer, "\n");
FCM::AutoPtr<FCM::IFCMCalloc> pCalloc = Coherent::Utils::GetCallocService(s_Callback);
ASSERT(pCalloc.m_Ptr != NULL);
FCM::StringRep16 outputString = Utils::ToString16(std::string(buffer), s_Callback);
outputConsoleService->Trace(outputString);
pCalloc->Free(outputString);
}
}
@dimitarcl
Copy link

Could use the result from the vsnprintf and guard against overflow

@omalinov
Copy link
Author

The vsnprintf will truncate everything after the 1023 symbol from the generated string, so isn't it guarded automatically?
Do you mean to display the entire message if we overflow?

I removed the string only function since we could reuse the one with the callback and it currently looks like this:

void Utils::Trace(FCM::PIFCMCallback pCallback, const char* fmt, ...)
{
	static const int TRACE_BUFFER_SIZE = 1024;

	FCM::AutoPtr<FCM::IFCMUnknown> pUnk;
	FCM::AutoPtr<Application::Service::IOutputConsoleService> consoleOutService;
	FCM::Result tempRes = pCallback->GetService(
		Application::Service::FLASHAPP_OUTPUT_CONSOLE_SERVICE,
		pUnk.m_Ptr);

	consoleOutService = pUnk;
	pUnk.Reset();

	if (consoleOutService)
	{
		va_list args;
		char buffer[TRACE_BUFFER_SIZE];

		va_start(args, fmt);
		// Will truncate overflowing buffer.
		vsnprintf(buffer, TRACE_BUFFER_SIZE, fmt, args);
		va_end(args);

		FCM::AutoPtr<FCM::IFCMCalloc> pCalloc =
			Coherent::Utils::GetCallocService(pCallback);
		ASSERT(pCalloc.m_Ptr != NULL);

		FCM::StringRep16 outputString =
			Utils::ToString16(std::string(buffer), pCallback);

		consoleOutService->Trace(outputString);
		pCalloc->Free(outputString);
	}
}

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment