@@ -201,7 +201,9 @@ void nano_serialize_next(nano_buf *buf, const SEXP object) {
201201 buf -> buf [0 ] = 0x7 ;
202202 buf -> buf [1 ] = registered ;
203203 buf -> buf [2 ] = special_bit ;
204- buf -> cur += registered ? 16 : 4 ;
204+
205+ const int reg = (int ) registered ;
206+ buf -> cur += reg ? 12 : 4 ;
205207
206208 struct R_outpstream_st output_stream ;
207209
@@ -216,18 +218,18 @@ void nano_serialize_next(nano_buf *buf, const SEXP object) {
216218 NANONEXT_SERIAL_VER ,
217219 NULL ,
218220 nano_write_bytes ,
219- registered ? nano_inHook : NULL ,
220- registered ? CAR (nano_klassString ) : R_NilValue
221+ reg ? nano_inHook : NULL ,
222+ reg ? CAR (nano_klassString ) : R_NilValue
221223 );
222224
223225 R_Serialize (object , & output_stream );
224226
225- if (registered && TAG (nano_refHook ) != R_NilValue ) {
226- uint64_t cursor = (uint64_t ) buf -> cur ;
227- memcpy (buf -> buf + 8 , & cursor , sizeof (uint64_t ));
227+ if (reg && TAG (nano_refHook ) != R_NilValue ) {
228+ const uint64_t cursor = (uint64_t ) buf -> cur ;
229+ memcpy (buf -> buf + 4 , & cursor , sizeof (uint64_t ));
228230 SEXP call , out ;
229231
230- if (registered == 1 ) {
232+ if (reg == 1 ) {
231233
232234 PROTECT (call = Rf_lcons (CAR (nano_refHook ), Rf_cons (TAG (nano_refHook ), R_NilValue )));
233235 PROTECT (out = R_UnwindProtect (eval_safe , call , rl_reset , NULL , NULL ));
@@ -302,7 +304,7 @@ void nano_serialize_xdr(nano_buf *buf, const SEXP object) {
302304
303305SEXP nano_unserialize (unsigned char * buf , const size_t sz ) {
304306
305- int registered = (int ) buf [1 ];
307+ const int reg = (int ) buf [1 ];
306308 uint64_t offset ;
307309 size_t cur ;
308310 SEXP reflist ;
@@ -316,26 +318,26 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
316318 cur = 0 ;
317319 goto resume ;
318320 case 0x7 :
319- if (registered ) {
320- offset = * ( uint64_t * ) ( buf + 8 );
321+ if (reg ) {
322+ memcpy ( & offset , buf + 4 , sizeof ( uint64_t ) );
321323 if (offset ) {
322324 SEXP raw , call ;
323- if (registered == 1 ) {
325+ if (reg == 1 ) {
324326 PROTECT (raw = Rf_allocVector (RAWSXP , sz - offset ));
325327 memcpy (STDVEC_DATAPTR (raw ), buf + offset , sz - offset );
326328 PROTECT (call = Rf_lcons (CADR (nano_refHook ), Rf_cons (raw , R_NilValue )));
327329 reflist = Rf_eval (call , R_GlobalEnv );
328330 SET_TAG (nano_refHook , reflist );
329331 UNPROTECT (2 );
330332 } else {
331- R_xlen_t llen = * (R_xlen_t * ) (buf + offset );
333+ R_xlen_t llen , xlen ;
334+ memcpy (& llen , buf + offset , sizeof (R_xlen_t ));
332335 cur = offset + sizeof (R_xlen_t );
333336 PROTECT (reflist = Rf_allocVector (VECSXP , llen ));
334337 SEXP out ;
335338 SEXP func = CADR (nano_refHook );
336- R_xlen_t xlen ;
337339 for (R_xlen_t i = 0 ; i < llen ; i ++ ) {
338- xlen = * ( R_xlen_t * ) ( buf + cur );
340+ memcpy ( & xlen , buf + cur , sizeof ( R_xlen_t ) );
339341 cur += sizeof (R_xlen_t );
340342 PROTECT (raw = Rf_allocVector (RAWSXP , xlen ));
341343 memcpy (STDVEC_DATAPTR (raw ), buf + cur , xlen );
@@ -350,7 +352,7 @@ SEXP nano_unserialize(unsigned char *buf, const size_t sz) {
350352
351353 }
352354 }
353- cur = 16 ;
355+ cur = 12 ;
354356 goto resume ;
355357 }
356358 offset = 0 ;
0 commit comments