Fossil

Check-in [a5835cac]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Attempt to limit the depth of recursion in markdown formatting.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a5835cac3b63d2d4fe5bfffcbe8bc99b64781124a87748c054eef2270cbf1cac
User & Date: drh 2019-09-24 18:04:02
Context
2019-09-24
18:16
Additional efforts to limit depth of recursion in markdown. check-in: cd5e9f26 user: drh tags: trunk
18:04
Attempt to limit the depth of recursion in markdown formatting. check-in: a5835cac user: drh tags: trunk
17:09
Fix a problem with the treatment of unterminated quoted attributes in HTML elements in the wiki formatter. check-in: 48276cfc user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/markdown.c.

151
152
153
154
155
156
157

158
159
160
161
162
163
164
...
297
298
299
300
301
302
303



304

305

306
307

308
309
310
311
312
313
314
315
316
317
318
319
320

321
322
323
324
325
326
327
...
554
555
556
557
558
559
560

561
562
563
564
565
566
567
568
569
570
...
592
593
594
595
596
597
598

599
600
601
602
603
604
605
606
607
608
...
632
633
634
635
636
637
638

639
640
641
642
643
644
645
646
647
648
649
...
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
....
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
....
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
....
2144
2145
2146
2147
2148
2149
2150

2151
2152
2153
2154
2155
2156
2157
....
2202
2203
2204
2205
2206
2207
2208

2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221


/* render -- structure containing one particular render */
struct render {
  struct mkd_renderer make;
  struct Blob refs;
  char_trigger active_char[256];

  int nBlobCache;                /* Number of entries in aBlobCache */
  struct Blob *aBlobCache[20];   /* Cache of Blobs available for reuse */
};


/* html_tag -- structure for quick HTML tag search (inspired from discount) */
struct html_tag {
................................................................................
  return bsearch(&key,
                 block_tags,
                 count(block_tags),
                 sizeof block_tags[0],
                 cmp_html_tag);
}






/* get a new working buffer from the cache or create one */

static struct Blob *new_work_buffer(struct render *rndr){
  struct Blob *ret;

  if( rndr->nBlobCache ){
    ret = rndr->aBlobCache[--rndr->nBlobCache];
  }else{
    ret = fossil_malloc(sizeof(*ret));
  }
  *ret = empty_blob;
  return ret;
}


/* release the given working buffer back to the cache */
static void release_work_buffer(struct render *rndr, struct Blob *buf){
  if( !buf ) return;

  blob_reset(buf);
  if( rndr->nBlobCache < sizeof(rndr->aBlobCache)/sizeof(rndr->aBlobCache[0]) ){
    rndr->aBlobCache[rndr->nBlobCache++] = buf;
  }else{
    fossil_free(buf);
  }
}
................................................................................
      i++;
      continue;
    }
    if( data[i]==c
     && data[i-1]!=' '
     && data[i-1]!='\t'
     && data[i-1]!='\n'

    ){
      work = new_work_buffer(rndr);
      if( !work ) return 0;
      parse_inline(work, rndr, data, i);
      r = rndr->make.emphasis(ob, work, c, rndr->make.opaque);
      release_work_buffer(rndr, work);
      return r ? i+1 : 0;
    }
  }
  return 0;
................................................................................
    if( i+1<size
     && data[i]==c
     && data[i+1]==c
     && i
     && data[i-1]!=' '
     && data[i-1]!='\t'
     && data[i-1]!='\n'

    ){
      work = new_work_buffer(rndr);
      if( !work ) return 0;
      parse_inline(work, rndr, data, i);
      r = rndr->make.double_emphasis(ob, work, c, rndr->make.opaque);
      release_work_buffer(rndr, work);
      return r ? i+2 : 0;
    }
    i++;
  }
................................................................................
      continue;
    }

    if( i+2<size
     && data[i+1]==c
     && data[i+2] == c
     && rndr->make.triple_emphasis

    ){
      /* triple symbol found */
      struct Blob *work = new_work_buffer(rndr);
      if( !work ) return 0;
      parse_inline(work, rndr, data, i);
      r = rndr->make.triple_emphasis(ob, work, c, rndr->make.opaque);
      release_work_buffer(rndr, work);
      return r ? i+3 : 0;
    }else if( i+1<size && data[i+1]==c ){
      /* double symbol found, handing over to emph1 */
      len = parse_emph1(ob, rndr, data-2, size+2, c);
................................................................................
  i++;

  /* skip any amount of whitespace or newline */
  /* (this is much more laxist than original markdown syntax) */
  while( i<size && (data[i]==' ' || data[i]=='\t' || data[i]=='\n') ){ i++; }

  /* allocate temporary buffers to store content, link and title */
  content = new_work_buffer(rndr);
  link = new_work_buffer(rndr);
  title = new_work_buffer(rndr);
  if( !title ) return 0;
  ret = 0; /* error if we don't get to the callback */

  /* inline style link */
  if( i<size && data[i]=='(' ){
    size_t span_end = i;
    while( span_end<size
     && !(data[span_end]==')' && (span_end==i || data[span_end-1]!='\\'))
................................................................................
  struct Blob *ob,
  struct render *rndr,
  char *data,
  size_t size
){
  size_t beg, end, pre;
  struct Blob *work = new_work_buffer(rndr);
  if( !work ) work = ob;

  beg = 0;
  while( beg<size ){
    for(end=beg+1; end<size && data[end-1]!='\n'; end++);
    pre = prefix_code(data+beg, end-beg);
    if( pre ){
      beg += pre; /* skipping prefix */
................................................................................
  }

  if( i<size && data[i]=='\n' ){
    align_size++;

    /* render the header row */
    head = new_work_buffer(rndr);
    if( head ){
      parse_table_row(head, rndr, data, head_end, 0, 0, MKD_CELL_HEAD);
    }

    /* parse alignments if provided */
    if( col && (aligns=fossil_malloc(align_size * sizeof *aligns))!=0 ){
      for(i=0; i<align_size; i++) aligns[i] = 0;
      col = 0;
      i = head_end+1;

................................................................................
  char *ib_data;
  Blob text = BLOB_INITIALIZER;

  /* filling the render structure */
  if( !rndrer ) return;
  rndr.make = *rndrer;
  rndr.nBlobCache = 0;

  rndr.refs = empty_blob;
  for(i=0; i<256; i++) rndr.active_char[i] = 0;
  if( (rndr.make.emphasis
    || rndr.make.double_emphasis
    || rndr.make.triple_emphasis)
   && rndr.make.emph_chars
  ){
................................................................................

  /* second pass: actual rendering */
  if( rndr.make.prolog ) rndr.make.prolog(ob, rndr.make.opaque);
  parse_block(ob, &rndr, blob_buffer(&text), blob_size(&text));
  if( rndr.make.epilog ) rndr.make.epilog(ob, rndr.make.opaque);

  /* clean-up */

  blob_reset(&text);
  lr = (struct link_ref *)blob_buffer(&rndr.refs);
  end = blob_size(&rndr.refs)/sizeof(struct link_ref);
  for(i=0; i<end; i++){
    blob_reset(&lr[i].id);
    blob_reset(&lr[i].link);
    blob_reset(&lr[i].title);
  }
  blob_reset(&rndr.refs);
  for(i=0; i<rndr.nBlobCache; i++){
    fossil_free(rndr.aBlobCache[i]);
  }
}







>







 







>
>
>
|
>
|
>


>













>







 







>


<







 







>


<







 







>



<







 







|
|
|
<







 







<







 







<
|
<







 







>







 







>













151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
...
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
...
562
563
564
565
566
567
568
569
570
571

572
573
574
575
576
577
578
...
600
601
602
603
604
605
606
607
608
609

610
611
612
613
614
615
616
...
640
641
642
643
644
645
646
647
648
649
650

651
652
653
654
655
656
657
...
996
997
998
999
1000
1001
1002
1003
1004
1005

1006
1007
1008
1009
1010
1011
1012
....
1389
1390
1391
1392
1393
1394
1395

1396
1397
1398
1399
1400
1401
1402
....
1897
1898
1899
1900
1901
1902
1903

1904

1905
1906
1907
1908
1909
1910
1911
....
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
....
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227


/* render -- structure containing one particular render */
struct render {
  struct mkd_renderer make;
  struct Blob refs;
  char_trigger active_char[256];
  int iDepth;                    /* Depth of recursion */
  int nBlobCache;                /* Number of entries in aBlobCache */
  struct Blob *aBlobCache[20];   /* Cache of Blobs available for reuse */
};


/* html_tag -- structure for quick HTML tag search (inspired from discount) */
struct html_tag {
................................................................................
  return bsearch(&key,
                 block_tags,
                 count(block_tags),
                 sizeof block_tags[0],
                 cmp_html_tag);
}

/* return true if recursion has gone too deep */
static int too_deep(struct render *rndr){
  return rndr->iDepth>200;
}

/* get a new working buffer from the cache or create one.  return NULL
** if failIfDeep is true and the depth of recursion has gone too deep. */
static struct Blob *new_work_buffer(struct render *rndr){
  struct Blob *ret;
  rndr->iDepth++;
  if( rndr->nBlobCache ){
    ret = rndr->aBlobCache[--rndr->nBlobCache];
  }else{
    ret = fossil_malloc(sizeof(*ret));
  }
  *ret = empty_blob;
  return ret;
}


/* release the given working buffer back to the cache */
static void release_work_buffer(struct render *rndr, struct Blob *buf){
  if( !buf ) return;
  rndr->iDepth--;
  blob_reset(buf);
  if( rndr->nBlobCache < sizeof(rndr->aBlobCache)/sizeof(rndr->aBlobCache[0]) ){
    rndr->aBlobCache[rndr->nBlobCache++] = buf;
  }else{
    fossil_free(buf);
  }
}
................................................................................
      i++;
      continue;
    }
    if( data[i]==c
     && data[i-1]!=' '
     && data[i-1]!='\t'
     && data[i-1]!='\n'
     && !too_deep(rndr)
    ){
      work = new_work_buffer(rndr);

      parse_inline(work, rndr, data, i);
      r = rndr->make.emphasis(ob, work, c, rndr->make.opaque);
      release_work_buffer(rndr, work);
      return r ? i+1 : 0;
    }
  }
  return 0;
................................................................................
    if( i+1<size
     && data[i]==c
     && data[i+1]==c
     && i
     && data[i-1]!=' '
     && data[i-1]!='\t'
     && data[i-1]!='\n'
     && !too_deep(rndr)
    ){
      work = new_work_buffer(rndr);

      parse_inline(work, rndr, data, i);
      r = rndr->make.double_emphasis(ob, work, c, rndr->make.opaque);
      release_work_buffer(rndr, work);
      return r ? i+2 : 0;
    }
    i++;
  }
................................................................................
      continue;
    }

    if( i+2<size
     && data[i+1]==c
     && data[i+2] == c
     && rndr->make.triple_emphasis
     && !too_deep(rndr)
    ){
      /* triple symbol found */
      struct Blob *work = new_work_buffer(rndr);

      parse_inline(work, rndr, data, i);
      r = rndr->make.triple_emphasis(ob, work, c, rndr->make.opaque);
      release_work_buffer(rndr, work);
      return r ? i+3 : 0;
    }else if( i+1<size && data[i+1]==c ){
      /* double symbol found, handing over to emph1 */
      len = parse_emph1(ob, rndr, data-2, size+2, c);
................................................................................
  i++;

  /* skip any amount of whitespace or newline */
  /* (this is much more laxist than original markdown syntax) */
  while( i<size && (data[i]==' ' || data[i]=='\t' || data[i]=='\n') ){ i++; }

  /* allocate temporary buffers to store content, link and title */
  title = new_work_buffer(rndr);
  content = new_work_buffer(rndr);
  link = new_work_buffer(rndr);

  ret = 0; /* error if we don't get to the callback */

  /* inline style link */
  if( i<size && data[i]=='(' ){
    size_t span_end = i;
    while( span_end<size
     && !(data[span_end]==')' && (span_end==i || data[span_end-1]!='\\'))
................................................................................
  struct Blob *ob,
  struct render *rndr,
  char *data,
  size_t size
){
  size_t beg, end, pre;
  struct Blob *work = new_work_buffer(rndr);


  beg = 0;
  while( beg<size ){
    for(end=beg+1; end<size && data[end-1]!='\n'; end++);
    pre = prefix_code(data+beg, end-beg);
    if( pre ){
      beg += pre; /* skipping prefix */
................................................................................
  }

  if( i<size && data[i]=='\n' ){
    align_size++;

    /* render the header row */
    head = new_work_buffer(rndr);

    parse_table_row(head, rndr, data, head_end, 0, 0, MKD_CELL_HEAD);


    /* parse alignments if provided */
    if( col && (aligns=fossil_malloc(align_size * sizeof *aligns))!=0 ){
      for(i=0; i<align_size; i++) aligns[i] = 0;
      col = 0;
      i = head_end+1;

................................................................................
  char *ib_data;
  Blob text = BLOB_INITIALIZER;

  /* filling the render structure */
  if( !rndrer ) return;
  rndr.make = *rndrer;
  rndr.nBlobCache = 0;
  rndr.iDepth = 0;
  rndr.refs = empty_blob;
  for(i=0; i<256; i++) rndr.active_char[i] = 0;
  if( (rndr.make.emphasis
    || rndr.make.double_emphasis
    || rndr.make.triple_emphasis)
   && rndr.make.emph_chars
  ){
................................................................................

  /* second pass: actual rendering */
  if( rndr.make.prolog ) rndr.make.prolog(ob, rndr.make.opaque);
  parse_block(ob, &rndr, blob_buffer(&text), blob_size(&text));
  if( rndr.make.epilog ) rndr.make.epilog(ob, rndr.make.opaque);

  /* clean-up */
  assert( rndr.iDepth==0 );
  blob_reset(&text);
  lr = (struct link_ref *)blob_buffer(&rndr.refs);
  end = blob_size(&rndr.refs)/sizeof(struct link_ref);
  for(i=0; i<end; i++){
    blob_reset(&lr[i].id);
    blob_reset(&lr[i].link);
    blob_reset(&lr[i].title);
  }
  blob_reset(&rndr.refs);
  for(i=0; i<rndr.nBlobCache; i++){
    fossil_free(rndr.aBlobCache[i]);
  }
}