Skip to content

Fix misc bugs/leaks - #215

Open
jschueller wants to merge 3 commits into
modelon-community:masterfrom
jschueller:misc
Open

Fix misc bugs/leaks#215
jschueller wants to merge 3 commits into
modelon-community:masterfrom
jschueller:misc

Conversation

@jschueller

Copy link
Copy Markdown
Contributor

Memory Leaks Fixed (4)

  • fmi_xml_context.c — XML parser handle leaked on fopen/read/parse errors (added fmi_xml_free_context before returns)
  • fmi3_import_capi.cinstanceName leaked on 6 error paths in 3 instantiation functions (free + NULL before return)
  • jm_portability.c:302tmpPath leaked on mkdtemp failure (free + NULL return)
  • jm_string_set.h:109 — On malloc failure after vector insert, string set was corrupted (allocate before insert)

Null Pointer Dereferences / Crashes Fixed (9)

  • fmi1_xml_parser.c:593 — Added return -1 after calloc failure
  • fmi_xml_terminals_and_icons.c:27 — Used cb->calloc instead of callbacks->calloc (NULL parameter)
  • fmi1/fmi3_xml_parser_util.c — Added if (context->parser) guard on XML_StopParser
  • fmi_xml_context.c:73 — Same XML_StopParser NULL guard
  • fmi1/fmi3_xml_parser.c — Check fmi*_xml_reserve_parse_buffer return before dereference
  • CAPI fmi1/fmi2/fmi3_capi.c — Check fmi_util_allocate_options return for NULL
  • fmi1_capi_me.c — Added fmu != NULL && fmu->c != NULL guards, nullify after free
  • ZIP fmi_zip_unzip.c, fmi_zip_zip.c — Added if (!callbacks) callbacks = jm_get_default_callbacks()
  • ZIP fmi_minizip.c:386zipClose(NULL) guarded with ternary check

Logic Bugs Fixed (6)

  • fmi1/fmi2_xml_query.c:58while(curCh || ...)while(curCh && (...)) in skip_space (was consuming all characters)
  • fmi1/fmi2_xml_query.c:269 — Duplicate kind==TRUE check → kind==FALSE (NOT FALSE was incorrectly FALSE)
  • fmi2_xml_variable.c:459variable->type != fmi2_base_type_real (pointer-to-enum comparing against NULL) → proper base type check with NULL guard
  • fmi1_xml_variable.c:1255assert(0)return -1 (use-after-free in release builds)
  • fmi_miniunz.c:359 — Removed unzCloseCurrentFile(uf) when file was never successfully opened
  • fmi_miniunz.c:448exit(-1)return 1

Memory / Resource Safety (6)

  • jm_portability.c:114dlerror() NULL → %s is UB (added NULL check with fallback)
  • jm_portability.c:457free()cb->free() (allocator mismatch)
  • jm_portability.c:216sprintfjm_snprintf
  • fmi2_enums.c:294, fmi3_enums.c:368 — Non-null-terminated buffer on edge case (bufSize > 0 check + force null terminate)
  • fmi2_import.c:115strcpy with unchecked remaining capacity (added capacity check before strcpy)
  • fmi3_import_convenience.c:356 — Missing message in default callback logger (added message to fprintf)

Null Pointer Defenses (3)

  • fmi1/fmi2/fmi3_import_variable_list.c — Added if (!a) return 0 guard before a->fmu dereference in join_var_list
  • fmi1_xml_cosim.c — Reverted bad edit that changed memcpy source to NULL (restored original code)

- XML: Fix XML parser handle leak on file/read/parse errors; fix null deref after calloc in fmi1_parser; fix callbacks-vs-cb null deref in terminals_and_icons; fix skip_space consuming all chars (|| vs &&); fix NOT FALSE evaluating as FALSE (duplicate condition); fix assert(0) reaching use-after-free in release builds; fix ptr-to-enum comparison in independent variable type check; add XML_StopParser NULL guards; fix parse buffer null deref
- CAPI: Fix unchecked fmi_util_allocate_options return (null deref on load); fix missing null guards in fmi1_capi_free_model_instance
- Import: Fix instanceName memory leaks on error paths in fmi3_import_capi; fix buffer overflow in fmi2_import (strcpy without capacity check); fix missing message in fmi3_default_callback_logger; fix null derefs in variable_list join functions
- ZIP: Fix null callbacks crash in unzip/zip; fix zipClose(NULL) in minizip; fix unzCloseCurrentFile on unopened file; replace exit(-1) with return
- Util: Fix dlerror NULL passed to %s (UB); fix allocator mismatch (free vs cb->free); fix tmpPath leak on mkdtemp failure; fix string_set corruption on malloc failure; fix unterminated buffer in enum to-string; replace sprintf with jm_snprintf
@jschueller jschueller changed the title Fix memory misc bugs/leaks Fix misc bugs/leaks May 27, 2026
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