Skip to content

Fix potential memory issues#213

Open
grantzhou wants to merge 6 commits into
Hornetlabs:synchdb-develfrom
grantzhou:synchdb-devel
Open

Fix potential memory issues#213
grantzhou wants to merge 6 commits into
Hornetlabs:synchdb-develfrom
grantzhou:synchdb-devel

Conversation

@grantzhou
Copy link
Copy Markdown
Contributor

Summary of Fixes:

  1. Buffer Overflow Prevention (src/backend/converter/format_converter.c):

    • Function: derive_decimal_string_from_byte
    • Issue: The function used a fixed-size stack buffer char digits[128] to store the decimal representation of a byte array. For arbitrarily large precision numbers (supported by Debezium/Java BigDecimal), this could overflow if the input byte array exceeded approximately 53 bytes.
    • Fix: Changed digits to be dynamically allocated (palloc) based on the input length (len * 3 + 1), ensuring it can hold any converted value. Added pfree(digits) before returning.
  2. Memory Leak Fix (src/backend/converter/format_converter.c):

    • Function: handle_base64_to_numeric_with_scale
    • Issue: The function called derive_decimal_string_from_byte which returns an allocated string (value), but never freed it after using it to format the buffer. This would cause a memory leak for every numeric value processed.
    • Fix: Added pfree(value) immediately after it is used.
  3. Crash Prevention & Memory Leak Fix (src/backend/synchdb/synchdb.c):

    • Function: dbz_engine_get_offset
    • Issue 1 (Crash): The code called *(env)->CallObjectMethod(...) to get a Java string. It checked for JNI exceptions, but did not check if the returned result was NULL (which can happen without an exception). Passing NULL to (*env)->GetStringUTFChars would
      cause a JVM crash.
    • Issue 2 (Leak): The local reference jdstdb (created via NewStringUTF) was not being deleted in either the exception handling block or the success path, leading to a JNI local reference leak.
    • Fix: Added an explicit check if (result == NULL) to handle null returns safely. Added (*env)->DeleteLocalRef(env, jdstdb) to all exit paths (exception, null-return, and success).

Additional Notes:

  • The codebase relies heavily on JNI. Ensure that the JVM is properly configured and that DBZ_ENGINE_JAR_FILE is accessible.
  • The OLR client uses a large buffer (64MB default). Monitor memory usage if running many OLR connectors.
  • Error handling in synchdb.c often sets a shared memory error message. Ensure monitoring tools check this state.

grantzhou and others added 6 commits January 13, 2026 12:37
- batch_change_handling.md: Seqeuence -> Sequence
- debezium_event_processor.md: likst -> like
- fdw_based_snapshot.md: guarenteed -> guaranteed
- non_native_datatype_handling.md: becasue -> because
- openlog_replicator_event_processor.md: Unlink -> Unlike, caluses -> clauses, accpeted -> accepted, likst -> like
- changelog.md: paraenthesis -> parenthesis
- remote_database_setups.md: designzted -> designated, catpure -> capture
- state_view.md: prepaaring -> preparing
- configure_snapshot_engine.md: postgre is reasdy -> postgres is ready
- default_datatype_mapping.md: dedcimal -> decimal

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Fix "None-existent" to "Non-existent" in error messages
- Fix "dedcimal" to "decimal" in postgres type mappings
- Fix incorrect function name in comment (init_sqlserver -> init_postgres)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant