@@ -376,7 +376,7 @@ void mcreq_wipe_packet(mc_PIPELINE *pipeline, mc_PACKET *packet)
376376 return ;
377377 }
378378
379- if (( packet -> flags & MCREQ_F_DETACHED ) || IS_STANDALONE_SPAN ( & packet -> kh_span ) ) {
379+ if (packet -> flags & MCREQ_F_DETACHED ) {
380380 free (SPAN_BUFFER (& packet -> u_value .single ));
381381 } else {
382382 netbuf_mblock_release (& pipeline -> nbmgr , & packet -> u_value .single );
@@ -651,34 +651,37 @@ void mcreq_set_cid(mc_PIPELINE *pipeline, mc_PACKET *packet, uint32_t cid)
651651 char * key = header_and_key + header_size ;
652652
653653 // parse old collection id and determine its length
654- uint32_t old_collection_id ;
655- int old_collection_id_length = leb128_decode ((uint8_t * )key , key_length , & old_collection_id );
654+ uint32_t old_collection_id = 0 ;
655+ int old_collection_id_length = 0 ;
656+ if ((packet -> flags & MCREQ_F_HASCID ) != 0 ) {
657+ old_collection_id_length = leb128_decode ((uint8_t * )key , key_length , & old_collection_id );
658+ }
656659
657660 // encode new collection id
658661 uint8_t collection_id [5 ] = {0 };
659662 int collection_id_length = leb128_encode (cid , collection_id );
660663
661664 // fix field lengths in the packet
662665 int diff = collection_id_length - old_collection_id_length ;
663- size_t new_header_and_key_size = old_span .size + diff ;
664666 req .request .bodylen = htonl (ntohl (req .request .bodylen ) + diff );
665- size_t new_klen = key_length + diff ;
667+ size_t new_key_length = key_length + diff ;
666668 if (req .request .magic == PROTOCOL_BINARY_AREQ ) {
667- req .request .keylen = (new_klen << 8 ) | (flexible_extras_length & 0xff );
669+ req .request .keylen = (new_key_length << 8 ) | (flexible_extras_length & 0xff );
668670 } else {
669- req .request .keylen = htons (new_klen );
671+ req .request .keylen = htons (new_key_length );
670672 }
671673
672674 // copy old header fields, with only collection id updated
673- netbuf_mblock_reserve (& pipeline -> nbmgr , & packet -> kh_span );
674- char * new_header_and_key = SPAN_BUFFER (& packet -> kh_span );
675+ char * new_header_and_key = malloc (old_span .size + diff );
676+ CREATE_STANDALONE_SPAN (& packet -> kh_span , new_header_and_key , old_span .size + diff );
677+
675678 const char * ptr = header_and_key ;
676679 memcpy (new_header_and_key , ptr , header_size );
680+ // update header with new length values
677681 memcpy (new_header_and_key , req .bytes , sizeof (req .bytes ));
678- ptr += header_size + old_collection_id_length ;
679682 memcpy (new_header_and_key + header_size , collection_id , collection_id_length );
680- memcpy ( new_header_and_key + header_size + collection_id_length , ptr ,
681- new_header_and_key_size - collection_id_length - header_size );
683+ ptr += header_size + old_collection_id_length ;
684+ memcpy ( new_header_and_key + header_size + collection_id_length , ptr , key_length - old_collection_id_length );
682685
683686 // deallocate the old span
684687 if (IS_STANDALONE_SPAN (& old_span )) {
@@ -689,6 +692,7 @@ void mcreq_set_cid(mc_PIPELINE *pipeline, mc_PACKET *packet, uint32_t cid)
689692 }
690693
691694 packet -> flags |= MCREQ_F_HASCID ;
695+ packet -> flags &= ~MCREQ_F_KEY_NOCOPY ;
692696}
693697
694698uint32_t mcreq_get_cid (lcb_INSTANCE * instance , const mc_PACKET * packet , int * cid_set )
@@ -725,7 +729,7 @@ uint32_t mcreq_get_cid(lcb_INSTANCE *instance, const mc_PACKET *packet, int *cid
725729 return 0 ;
726730}
727731
728- void mcreq_get_key (lcb_INSTANCE * instance , const mc_PACKET * packet , const char * * key , size_t * nkey )
732+ void mcreq_get_key (const mc_PACKET * packet , const char * * key , size_t * nkey )
729733{
730734 uint8_t ffext = 0 ;
731735 uint16_t nk = 0 ;
@@ -743,7 +747,7 @@ void mcreq_get_key(lcb_INSTANCE *instance, const mc_PACKET *packet, const char *
743747 nk = ntohs (req .request .keylen );
744748 }
745749 k = kh + sizeof (req ) + req .request .extlen + ffext ;
746- if ((packet -> flags & MCREQ_F_NOCID ) == 0 && instance && LCBT_SETTING ( instance , use_collections ) ) {
750+ if ((packet -> flags & MCREQ_F_HASCID ) != 0 ) {
747751 ncid = leb128_decode ((uint8_t * )k , nk , & cid );
748752 (void )cid ;
749753 }
0 commit comments