comparison lwasm/pseudo.c @ 568:b549a3a417a9

Fix some additional glitches in implementation of includebin offset/length The usage of the "len" element of the line structure to pass information forward is invalid and may cause weird problems. Instead, use the two "lint" and "lint2" values to pass information forward and only set the "len" element when the final length is known.
author William Astle <lost@l-w.ca>
date Thu, 21 Dec 2023 22:31:53 -0700
parents 71ba873a0ec6
children 2ac2d447a2fa
comparison
equal deleted inserted replaced
567:71ba873a0ec6 568:b549a3a417a9
1520 1520
1521 fseek(fp, 0, SEEK_END); 1521 fseek(fp, 0, SEEK_END);
1522 flen = ftell(fp); 1522 flen = ftell(fp);
1523 fclose(fp); 1523 fclose(fp);
1524 1524
1525 l -> len = flen; 1525 l -> lint2 = flen;
1526 1526
1527 if (**p == ',') 1527 if (**p == ',')
1528 { 1528 {
1529 (*p)++; 1529 (*p)++;
1530 e = lwasm_parse_expr(as, p); 1530 e = lwasm_parse_expr(as, p);
1538 e1 = lwasm_parse_expr(as, p); 1538 e1 = lwasm_parse_expr(as, p);
1539 1539
1540 if (e1) 1540 if (e1)
1541 lwasm_save_expr(l, 1, e1); 1541 lwasm_save_expr(l, 1, e1);
1542 } 1542 }
1543 }
1544 else
1545 {
1546 l -> len = flen; // length is resolved
1543 } 1547 }
1544 } 1548 }
1545 1549
1546 RESOLVEFUNC(pseudo_resolve_includebin) 1550 RESOLVEFUNC(pseudo_resolve_includebin)
1547 { 1551 {
1560 if (e != NULL) 1564 if (e != NULL)
1561 { 1565 {
1562 i = lw_expr_intval(e); 1566 i = lw_expr_intval(e);
1563 1567
1564 if (i < 0) 1568 if (i < 0)
1565 i = l -> len + i; 1569 i = l -> lint2 + i;
1566 } 1570 }
1567 1571
1568 i1 = l -> len - i; 1572 i1 = l -> lint2 - i;
1569 1573
1570 e1 = lwasm_fetch_expr(l, 1); 1574 e1 = lwasm_fetch_expr(l, 1);
1571 1575
1572 if (e1 != NULL) 1576 if (e1 != NULL)
1573 { 1577 {
1579 /* starting before file */ 1583 /* starting before file */
1580 lwasm_register_error(as, l, E_INCLUDEBIN_ILL_START); 1584 lwasm_register_error(as, l, E_INCLUDEBIN_ILL_START);
1581 return; 1585 return;
1582 } 1586 }
1583 1587
1584 if (i > l -> len) 1588 if (i > l -> lint2)
1585 { 1589 {
1586 /* starts past end of file */ 1590 /* starts past end of file */
1587 lwasm_register_error(as, l, E_INCLUDEBIN_ILL_START); 1591 lwasm_register_error(as, l, E_INCLUDEBIN_ILL_START);
1588 return; 1592 return;
1589 } 1593 }
1593 /* length is negative */ 1597 /* length is negative */
1594 lwasm_register_error(as, l, E_INCLUDEBIN_ILL_LENGTH); 1598 lwasm_register_error(as, l, E_INCLUDEBIN_ILL_LENGTH);
1595 return; 1599 return;
1596 } 1600 }
1597 1601
1598 if (i + i1 > l -> len) 1602 if (i + i1 > l -> lint2)
1599 { 1603 {
1600 /* read past end of file */ 1604 /* read past end of file */
1601 lwasm_register_error(as, l, E_INCLUDEBIN_ILL_LENGTH); 1605 lwasm_register_error(as, l, E_INCLUDEBIN_ILL_LENGTH);
1602 return; 1606 return;
1603 } 1607 }