Skip to content

Instantly share code, notes, and snippets.

@ericek111
Created November 3, 2025 08:21
Show Gist options
  • Select an option

  • Save ericek111/105c1318a46baf987992acef38460359 to your computer and use it in GitHub Desktop.

Select an option

Save ericek111/105c1318a46baf987992acef38460359 to your computer and use it in GitHub Desktop.
Work around a bug in Shared Flight (X-Plane 12 addon) on Linux that causes a crash by calling .back() on an empty string ("").
// g++ -g -Wall -shared -fPIC -ldl -D_GLIBCXX_USE_CXX11_ABI=0 std-string-back-patch.cpp -o stringbackpatch.so
// then, in Steam: LD_PRELOAD="/path/to/stringbackpatch.so" %command%
#ifndef _GNU_SOURCE
#define _GNU_SOURCE // necessary for RTLD_NEXT in dlfcn.h
#endif
#include <dlfcn.h>
#include <stdio.h>
#include <string>
typedef void* (*origback_t)(std::string* a1);
origback_t sf_orig_std_string_back = NULL;
char empty_char = '\0';
extern "C" void* _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4backEv(std::string* a1) {
if (sf_orig_std_string_back == NULL) {
sf_orig_std_string_back = (origback_t) dlsym(RTLD_NEXT, "_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE4backEv");
}
if (a1 == NULL || a1->c_str()[0] == '\0') {
printf(" -> Called std::string.back() on an empty string!!!\n");
return &empty_char;
}
return sf_orig_std_string_back(a1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment