Skip to content

Commit

Permalink
Fix segfault calling YGJNILogFunc (facebook#1344)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#1344

`YGJNILogFunc` has a bug where it uses a `va_list` to determine the length of a printf string, then reuses the same `va_list` later after it has already been iterated through. Even if no arguments are present, this may cause a crash looking something like:

```
C  [libsystem_platform.dylib+0xf12]  _platform_strlen+0x12
C  [libsystem_c.dylib+0x31bf]  __vfprintf+0x1339
C  [libsystem_c.dylib+0x307ce]  _vsnprintf+0x100
C  [libsystem_c.dylib+0x6965]  vsnprintf+0x44
C  [libyoga.dylib+0x5161]  YGJNILogFunc(YGConfig*, YGNode*, YGLogLevel, void*, char const*, __va_list_tag*)+0x59
```

Fixing this fixes crashing unit tests which are not explicitly disabled.

Reviewed By: yungsters

Differential Revision: D48388548

fbshipit-source-id: c80a703b9a294665f1305361b382c99b978fe825
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Aug 16, 2023
1 parent 910adaa commit 3fd0f96
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion java/jni/YGJNIVanilla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ static int YGJNILogFunc(
void* /*layoutContext*/,
const char* format,
va_list args) {
int result = vsnprintf(NULL, 0, format, args);
va_list argsCopy;
va_copy(argsCopy, args);
int result = vsnprintf(nullptr, 0, format, argsCopy);
std::vector<char> buffer(1 + result);
vsnprintf(buffer.data(), buffer.size(), format, args);

Expand Down

0 comments on commit 3fd0f96

Please sign in to comment.