Skip to content

Commit

Permalink
Allow mio_new_mio() to have size 0
Browse files Browse the repository at this point in the history
Consider for instance the following HTML code where a subparser is called
on the contents of the <script> tags.

<script language="Javascript" src="./test.js"></script>
<script language="JavaScript">
...
</script>

Since the first <script> is empty, mio_new_mio() is called with 0 size
but with the current semantics of the API it creates MIO until the end of
the file so the whole source file to EOF is parsed. While it seems
no extra tags are generated in this case, the parsing happens
unnecessarily.

Instead of using 0, make size signed and use -1 when we want to create a
MIO till the end of the source MIO.
  • Loading branch information
techee committed Dec 14, 2018
1 parent 7dd0253 commit 915d697
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions main/mio.c
Expand Up @@ -361,20 +361,20 @@ MIO *mio_new_memory (unsigned char *data,
* @size: the length of the data copied from @base to new mio
*
* Creates a new #MIO object by copying data from existing #MIO (@base).
* The range for copying are given with @start and @size.
* The range for copying is given with @start and @size.
* Copying data at the range from @start to the end of @base is
* done if 0 is given as @size.
* done if -1 is given as @size.
*
* If @size(!= 0) is larger than the length from @start to the end of
* @base, %NULL is return.
* If @size is larger than the length from @start to the end of
* @base, %NULL is returned.
*
* The function doesn't move the file position of @base.
*
* Free-function: mio_free()
*
*/

MIO *mio_new_mio (MIO *base, long start, size_t size)
MIO *mio_new_mio (MIO *base, long start, long size)
{
unsigned char *data;
long original_pos;
Expand All @@ -383,7 +383,7 @@ MIO *mio_new_mio (MIO *base, long start, size_t size)

original_pos = mio_tell (base);

if (size == 0)
if (size == -1)
{
long end;

Expand Down
2 changes: 1 addition & 1 deletion main/mio.h
Expand Up @@ -123,7 +123,7 @@ MIO *mio_new_memory (unsigned char *data,
MIOReallocFunc realloc_func,
MIODestroyNotify free_func);

MIO *mio_new_mio (MIO *base, long start, size_t size);
MIO *mio_new_mio (MIO *base, long start, long size);
MIO *mio_ref (MIO *mio);

int mio_free (MIO *mio);
Expand Down
2 changes: 1 addition & 1 deletion main/parse.c
Expand Up @@ -854,7 +854,7 @@ struct getLangCtx {
(mio_memory_get_data((_glc_)->input, NULL) == NULL)) \
{ \
MIO *tmp_ = (_glc_)->input; \
(_glc_)->input = mio_new_mio (tmp_, 0, 0); \
(_glc_)->input = mio_new_mio (tmp_, 0, -1); \
mio_free (tmp_); \
if (!(_glc_)->input) { \
(_glc_)->err = true; \
Expand Down

0 comments on commit 915d697

Please sign in to comment.