Skip to content

Commit

Permalink
8213622: Windows VS2013 build failure - "'snprintf': identifier not f…
Browse files Browse the repository at this point in the history
…ound"

Replace snprintf with strlen and memcpy

Backport-of: d7d4bc9
  • Loading branch information
Amos Shi authored and GoeLin committed Dec 4, 2023
1 parent b9b01c9 commit 4b88beb
Showing 1 changed file with 28 additions and 7 deletions.
Expand Up @@ -23,6 +23,7 @@
*/

#include <stdlib.h>
#include <string.h>

#include "ExceptionCheckingJniEnv.hpp"

Expand All @@ -48,20 +49,40 @@ class JNIVerifier {
}

void ProcessReturnError() {
int len = snprintf(NULL, 0, "%s : %s", _base_msg, _return_error) + 1;

if (len <= 0) {
_env->HandleError(_return_error);
return;
}
// This is error prone, but:
// - Seems like we cannot use std::string (due to windows/solaris not
// building when used, seemingly due to exception libraries not linking).
// - Seems like we cannot use sprintf due to VS2013 (JDK-8213622).
//
// We are aiming to do:
// snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
// but will use strlen + memcpy instead.
size_t base_len = strlen(_base_msg);
const char* between_msg = " : ";
size_t between_len = strlen(between_msg);
size_t return_len = strlen(_return_error);

// +1 for the '\0'
size_t len = base_len + between_len + return_len + 1;

char* full_message = (char*) malloc(len);
if (full_message == NULL) {
_env->HandleError(_return_error);
return;
}

snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
// Now we construct the string using memcpy to not use sprintf/std::string
// instead of:
// snprintf(full_message, len, "%s : %s", _base_msg, _return_error);
memcpy(full_message, _base_msg, base_len);
memcpy(full_message + base_len, between_msg, between_len);
memcpy(full_message + base_len + between_len, _return_error, return_len);
full_message[len - 1] = '\0';

// -1 due to the '\0' not counted by strlen but is counted for the allocation.
if (strlen(full_message) != len - 1) {
_env->GetJNIEnv()->FatalError("Length of message is not what was expected");
}

_env->HandleError(full_message);
free(full_message);
Expand Down

1 comment on commit 4b88beb

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.