@@ -367,7 +367,7 @@ static int pushstreamonly(lua_State * L, ppstream * stream)
367367
368368static int pushreference (lua_State * L , ppref * reference )
369369{
370- if (reference != NULL ) {
370+ if (reference != NULL && reference -> number != 0 ) {
371371 pdfe_push_reference ;
372372 lua_pushinteger (L ,reference -> number );
373373 return 2 ;
@@ -502,8 +502,10 @@ static int pdfelib_getfromarray(lua_State * L)
502502 int index = luaL_checkint (L , 2 ) - 1 ;
503503 if (index < a -> array -> size ) {
504504 ppobj * object = pparray_at (a -> array ,index );
505- lua_pushinteger (L ,(int ) object -> type );
506- return 1 + pushvalue (L ,object );
505+ if (object != NULL ) {
506+ lua_pushinteger (L ,(int ) object -> type );
507+ return 1 + pushvalue (L ,object );
508+ }
507509 }
508510 }
509511 return 0 ;
@@ -524,10 +526,12 @@ static int pdfelib_getfromdictionary(lua_State * L)
524526 int index = luaL_checkint (L , 2 ) - 1 ;
525527 if (index < d -> dictionary -> size ) {
526528 ppobj * object = ppdict_at (d -> dictionary ,index );
527- ppname key = ppdict_key (d -> dictionary ,index );
528- lua_pushstring (L ,(const char * ) key );
529- lua_pushinteger (L ,(int ) object -> type );
530- return 2 + pushvalue (L ,object );
529+ if (object != NULL ) {
530+ ppname key = ppdict_key (d -> dictionary ,index );
531+ lua_pushstring (L ,(const char * ) key );
532+ lua_pushinteger (L ,(int ) object -> type );
533+ return 2 + pushvalue (L ,object );
534+ }
531535 }
532536 }
533537 }
@@ -550,10 +554,12 @@ static int pdfelib_getfromstream(lua_State * L)
550554 int index = luaL_checkint (L , 2 ) - 1 ;
551555 if (index < d -> size ) {
552556 ppobj * object = ppdict_at (d ,index );
553- ppname key = ppdict_key (d ,index );
554- lua_pushstring (L ,(const char * ) key );
555- lua_pushinteger (L ,(int ) object -> type );
556- return 2 + pushvalue (L ,object );
557+ if (object != NULL ) {
558+ ppname key = ppdict_key (d ,index );
559+ lua_pushstring (L ,(const char * ) key );
560+ lua_pushinteger (L ,(int ) object -> type );
561+ return 2 + pushvalue (L ,object );
562+ }
557563 }
558564 }
559565 }
@@ -622,14 +628,17 @@ static int pdfelib_arraytotable(lua_State * L)
622628 if (a != NULL ) {
623629 int flat = lua_isboolean (L ,2 );
624630 int i = 0 ;
631+ int j = 0 ;
625632 lua_createtable (L ,i ,0 );
626633 /* table */
627634 for (i = 0 ;i < a -> array -> size ;i ++ ) {
628635 ppobj * object = pparray_at (a -> array ,i );
629- pdfelib_totable (L ,object ,flat );
630- /* table { type, [value], [extra], [more] } */
631- lua_rawseti (L ,-2 ,i + 1 );
632- /* table[i] = { type, [value], [extra], [more] } */
636+ if (object != NULL ) {
637+ pdfelib_totable (L ,object ,flat );
638+ /* table { type, [value], [extra], [more] } */
639+ lua_rawseti (L ,-2 ,++ j );
640+ /* table[i] = { type, [value], [extra], [more] } */
641+ }
633642 }
634643 return 1 ;
635644 }
@@ -646,13 +655,15 @@ static int pdfelib_dictionarytotable(lua_State * L)
646655 /* table */
647656 for (i = 0 ;i < d -> dictionary -> size ;i ++ ) {
648657 ppobj * object = ppdict_at (d -> dictionary ,i );
649- ppname key = ppdict_key (d -> dictionary ,i );
650- lua_pushstring (L ,(const char * ) key );
651- /* table key */
652- pdfelib_totable (L ,object ,flat );
653- /* table key { type, [value], [extra], [more] } */
654- lua_rawset (L ,-3 );
655- /* table[key] = { type, [value], [extra] } */
658+ if (object != NULL ) {
659+ ppname key = ppdict_key (d -> dictionary ,i );
660+ lua_pushstring (L ,(const char * ) key );
661+ /* table key */
662+ pdfelib_totable (L ,object ,flat );
663+ /* table key { type, [value], [extra], [more] } */
664+ lua_rawset (L ,-3 );
665+ /* table[key] = { type, [value], [extra] } */
666+ }
656667 }
657668 return 1 ;
658669 }
@@ -676,22 +687,25 @@ static int pdfelib_pagestotable(lua_State * L)
676687 ppdoc * d = p -> document ;
677688 ppref * r = NULL ;
678689 int i = 0 ;
690+ int j = 0 ;
679691 lua_createtable (L ,ppdoc_page_count (d ),0 );
680692 /* pages[1..n] */
681693 for (r = ppdoc_first_page (d ), i = 1 ; r != NULL ; r = ppdoc_next_page (d ), ++ i ) {
682694 lua_createtable (L ,3 ,0 );
683- pushdictionary (L ,ppref_obj (r )-> dict );
684- /* table dictionary n */
685- lua_rawseti (L ,-3 ,2 );
686- /* table dictionary */
687- lua_rawseti (L ,-2 ,1 );
688- /* table */
689- lua_pushinteger (L ,r -> number );
690- /* table reference */
691- lua_rawseti (L ,-2 ,3 );
692- /* table */
693- lua_rawseti (L ,-2 ,i );
694- /* pages[i] = { dictionary, size, objnum } */
695+ if (ppref_obj (r ) != NULL ) {
696+ pushdictionary (L ,ppref_obj (r )-> dict );
697+ /* table dictionary n */
698+ lua_rawseti (L ,-3 ,2 );
699+ /* table dictionary */
700+ lua_rawseti (L ,-2 ,1 );
701+ /* table */
702+ lua_pushinteger (L ,r -> number );
703+ /* table reference */
704+ lua_rawseti (L ,-2 ,3 );
705+ /* table */
706+ lua_rawseti (L ,-2 ,++ j );
707+ /* pages[i] = { dictionary, size, objnum } */
708+ }
695709 }
696710 return 1 ;
697711 }
@@ -1158,10 +1172,15 @@ static int pdfelib_getbox(lua_State * L)
11581172static int pdfelib_getfromreference (lua_State * L )
11591173{
11601174 pdfe_reference * r = check_isreference (L , 1 );
1161- if (r != NULL ) {
1162- ppobj * o = ppref_obj (ppxref_find (r -> xref , (ppuint ) r -> onum ));
1163- lua_pushinteger (L ,o -> type );
1164- return 1 + pushvalue (L ,o );
1175+ if (r != NULL && r -> xref != NULL ) {
1176+ ppref * rr = ppxref_find (r -> xref , (ppuint ) r -> onum );
1177+ if (rr != NULL ) {
1178+ ppobj * o = ppref_obj (rr );
1179+ if (o != NULL ) {
1180+ lua_pushinteger (L ,o -> type );
1181+ return 1 + pushvalue (L ,o );
1182+ }
1183+ }
11651184 }
11661185 return 0 ;
11671186}
@@ -1213,8 +1232,8 @@ static int pdfelib_getfromreference(lua_State * L)
12131232*/
12141233
12151234# define pdfelib_get_indirect_o (p ) \
1216- ppref *r = ppxref_find(((pdfe_reference *) p)->xref, (ppuint) (((pdfe_reference *) p)->onum)); \
1217- ppobj *o = ppref_obj(r); \
1235+ ppref *r = (((pdfe_reference *) p)->xref != NULL) ? ppxref_find(((pdfe_reference *) p)->xref, (ppuint) (((pdfe_reference *) p)->onum)) : NULL ; \
1236+ ppobj *o = (r != NULL) ? ppref_obj(r) : NULL; \
12181237
12191238# define pdfelib_get_value_direct (get_d ,get_a ) do { \
12201239 int t = lua_type(L,2); \
0 commit comments