{"record":{"id":"451911f9a9cb4247","repo":"Tencent/matrix","slug":"unable-to-allocate-memory-for-a-new-configuration","errorCode":null,"errorMessage":"Unable to allocate memory for a new configuration.","messagePattern":"Unable to allocate memory for a new configuration\\.","errorType":"console","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"matrix/matrix-android/matrix-sqlite-lint/src/lemon/lemon-gen/lemon.c","lineNumber":1088,"sourceCode":"** Routines to processing a configuration list and building a state\n** in the LEMON parser generator.\n*/\n\nstatic struct config *freelist = 0;      /* List of free configurations */\nstatic struct config *current = 0;       /* Top of list of configurations */\nstatic struct config **currentend = 0;   /* Last on list of configs */\nstatic struct config *basis = 0;         /* Top of list of basis configs */\nstatic struct config **basisend = 0;     /* End of list of basis configs */\n\n/* Return a pointer to a new configuration */\nPRIVATE struct config *newconfig(){\n  struct config *new;\n  if( freelist==0 ){\n    int i;\n    int amt = 3;\n    freelist = (struct config *)malloc( sizeof(struct config)*amt );\n    if( freelist==0 ){\n      fprintf(stderr,\"Unable to allocate memory for a new configuration.\");\n      exit(1);\n    }\n    for(i=0; i<amt-1; i++) freelist[i].next = &freelist[i+1];\n    freelist[amt-1].next = 0;\n  }\n  new = freelist;\n  freelist = freelist->next;\n  return new;\n}\n\n/* The configuration \"old\" is no longer used */\nPRIVATE void deleteconfig(old)\nstruct config *old;\n{\n  old->next = freelist;\n  freelist = old;\n}\n","sourceCodeStart":1070,"sourceCodeEnd":1106,"githubUrl":"https://github.com/Tencent/matrix/blob/3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7/matrix/matrix-android/matrix-sqlite-lint/src/lemon/lemon-gen/lemon.c#L1070-L1106","documentation":"This fatal error is printed by the lemon parser generator when malloc() fails to allocate a block for three new LALR(1) configuration structs used to seed the internal free-list. The library cannot recover from losing its configuration pool, so it prints this message to stderr and calls exit(1). It indicates the host machine has exhausted memory available to the process.","triggerScenarios":"malloc(sizeof(struct config)*3) returns NULL at lemon.c:1088 when the config free-list is empty and a new config is requested via Configlist_add/Confignew, because system memory or the malloc arena is exhausted.","commonSituations":"Running lemon on grammar files so large that earlier allocations already consumed available memory; running inside a container, CI job, or ulimit-restricted shell with a tiny memory cgroup; 32-bit builds where the heap address space is exhausted; severe system-wide memory pressure from other processes.","solutions":["Free up memory or raise the memory limit (ulimit -v / container memory cgroup / swap) and rerun lemon.","Check the grammar for runaway growth (huge rule counts, accidental repetition) that inflates the configuration set.","Move the build to a machine with more RAM or a 64-bit toolchain.","If leaks elsewhere in lemon are implicated, patch lemon.c to use calloc/realloc more defensively or track peak usage.","Rebuild with a malloc that aborts loudly or uses overcommit to distinguish genuine OOM from limits."],"exampleFix":"// before\nfreelist = (struct config *)malloc( sizeof(struct config)*amt );\nif( freelist==0 ){\n  fprintf(stderr,\"Unable to allocate memory for a new configuration.\");\n  exit(1);\n}\n// after\nfreelist = (struct config *)calloc( amt, sizeof(struct config) );\nif( freelist==0 ){\n  fprintf(stderr,\"Out of memory allocating %d configs.\\n\", amt);\n  exit(1);\n}","handlingStrategy":"try-catch","validationCode":"// before running lemon, check available memory vs. a floor\nlong pages = sysconf(_SC_AVPHYS_PAGES), ps = sysconf(_SC_PAGESIZE);\nif (pages * ps < 64UL*1024*1024) { fprintf(stderr,\"not enough free memory to run lemon\\n\"); return 1; }","typeGuard":null,"tryCatchPattern":"// C has no try/catch; the library exits(1). Wrap the process call:\nint rc = system(\"lemon grammar.y\");\nif (rc != 0) { fprintf(stderr,\"lemon failed (possibly OOM); freeing resources and retrying once\\n\"); /* free memory, raise limits, retry */ }","preventionTips":["Set a generous ulimit -v or container memory limit before builds","Monitor free memory in CI before code-generation steps","Keep grammars modest in size and split oversized ones","Run lemon in a 64-bit build to avoid address-space exhaustion"],"tags":["c","memory-allocation","parser-generator","out-of-memory"],"backgroundTag":"out-of-memory","analyzedSha":"3b8293bd65d47eeea7caf1f32a3a5d4d5eab60e7","analyzedAt":"2026-09-08T08:01:39.722Z","contentChangedAt":"2026-09-08T08:01:39.722Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}