owned == NULL removes the whole span (the historical contract); otherwise * only the tables it declares go and the rest of the span stays (#2228). */
| 1148 | /* owned == NULL removes the whole span (the historical contract); otherwise |
| 1149 | * only the tables it declares go and the rest of the span stays (#2228). */ |
| 1150 | static int toml_remove_managed(const char *file_path, const char *begin_marker, |
| 1151 | const char *end_marker, const char *owned) { |
| 1152 | size_t owned_len = 0U; |
| 1153 | if (!toml_valid_path(file_path) || !toml_valid_marker(begin_marker) || |
| 1154 | !toml_valid_marker(end_marker) || strcmp(begin_marker, end_marker) == 0 || |
| 1155 | (owned && !toml_managed_body_valid(owned, begin_marker, end_marker, &owned_len))) { |
| 1156 | return TOML_EDIT_ERR; |
| 1157 | } |
| 1158 | char *existing = NULL; |
| 1159 | size_t existing_len = 0; |
| 1160 | toml_file_snapshot_t snapshot; |
| 1161 | if (toml_read_file(file_path, &existing, &existing_len, &snapshot) != TOML_EDIT_OK || |
| 1162 | !toml_text_is_safe(existing, existing_len, 1)) { |
| 1163 | free(existing); |
| 1164 | return TOML_EDIT_ERR; |
| 1165 | } |
| 1166 | |
| 1167 | toml_line_t begin_line = {0}; |
| 1168 | toml_line_t end_line = {0}; |
| 1169 | int has_pair = 0; |
| 1170 | int find_rc = toml_find_markers(existing, existing_len, begin_marker, end_marker, &begin_line, |
| 1171 | &end_line, &has_pair); |
| 1172 | if (find_rc == TOML_EDIT_ORPHAN_MARKER) { |
| 1173 | /* Self-heal: drop the stray marker line. Whichever side survived is the |
| 1174 | * one toml_find_markers recorded, so the line bounds are known exactly |
| 1175 | * — nothing is guessed, and no surrounding content is touched. */ |
| 1176 | const toml_line_t *stray = begin_line.full_end > 0U ? &begin_line : &end_line; |
| 1177 | toml_buffer_t healed = {0}; |
| 1178 | if (toml_buffer_append(&healed, existing, stray->start) != TOML_EDIT_OK || |
| 1179 | toml_buffer_append(&healed, existing + stray->full_end, |
| 1180 | existing_len - stray->full_end) != TOML_EDIT_OK) { |
| 1181 | toml_buffer_dispose(&healed); |
| 1182 | free(existing); |
| 1183 | return TOML_EDIT_ERR; |
| 1184 | } |
| 1185 | int healed_rc = toml_write_atomic(file_path, existing, existing_len, |
| 1186 | healed.data ? healed.data : "", healed.len, &snapshot); |
| 1187 | toml_buffer_dispose(&healed); |
| 1188 | free(existing); |
| 1189 | return healed_rc; |
| 1190 | } |
| 1191 | if (find_rc != TOML_EDIT_OK) { |
| 1192 | free(existing); |
| 1193 | return TOML_EDIT_ERR; |
| 1194 | } |
| 1195 | if (!has_pair) { |
| 1196 | free(existing); |
| 1197 | return TOML_EDIT_OK; |
| 1198 | } |
| 1199 | |
| 1200 | toml_buffer_t output = {0}; |
| 1201 | size_t prefix_len = begin_line.start; |
| 1202 | if (prefix_len == 0U && existing_len >= 3U && (unsigned char)existing[0] == 0xefU && |
| 1203 | (unsigned char)existing[1] == 0xbbU && (unsigned char)existing[2] == 0xbfU) { |
| 1204 | prefix_len = 3U; |
| 1205 | } |
| 1206 | int built = owned ? toml_managed_rewrite(existing, existing_len, &begin_line, &end_line, |
| 1207 | begin_marker, end_marker, owned, owned_len, 0, &output) |
no test coverage detected