Skip to content

Commit 810e02f

Browse files
authored
Fix Issue#1235: Sanitize XML comment to prevent invalid token errors (#1783)
Original description: Pull Requests Description : Added logic to detect and replace any occurrence of "--" in comments with a single "-" to ensure valid XML. Used a bulk write ('fwrite') to efficiently handle portions of the string that don't contain invalid sequences. Ensured that comments are written correctly without altering the original structure of the code. Updated function 'write_spucomment' to handle the sanitization process efficiently.
1 parent 2720448 commit 810e02f

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/lib_ccx/ccx_encoders_spupng.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,38 @@ void write_sputag_close(struct spupng_t *sp)
186186
}
187187
void write_spucomment(struct spupng_t *sp, const char *str)
188188
{
189-
fprintf(sp->fpxml, "<!--\n%s\n-->\n", str);
189+
fprintf(sp->fpxml, "<!--\n");
190+
191+
const char *p = str;
192+
const char *last_safe_pos = str; // Track the last safe position to flush
193+
194+
while (*p)
195+
{
196+
197+
if (*p == '-' && *(p + 1) == '-')
198+
{
199+
200+
if (p > last_safe_pos)
201+
{
202+
fwrite(last_safe_pos, 1, p - last_safe_pos, sp->fpxml);
203+
}
204+
205+
fputc('-', sp->fpxml);
206+
p += 2;
207+
last_safe_pos = p;
208+
}
209+
else
210+
{
211+
p++;
212+
}
213+
}
214+
215+
if (p > last_safe_pos)
216+
{
217+
fwrite(last_safe_pos, 1, p - last_safe_pos, sp->fpxml);
218+
}
219+
220+
fprintf(sp->fpxml, "\n-->\n");
190221
}
191222

192223
char *get_spupng_filename(void *ctx)

0 commit comments

Comments
 (0)