@@ -702,9 +702,16 @@ php_mysqlnd_auth_response_read(MYSQLND_CONN_DATA * conn, void * _packet)
702702 if (0xFE == packet -> response_code ) {
703703 /* Authentication Switch Response */
704704 if (packet -> header .size > (size_t ) (p - buf )) {
705- packet -> new_auth_protocol = mnd_pestrdup ((char * )p , FALSE);
706- packet -> new_auth_protocol_len = strlen (packet -> new_auth_protocol );
707- p += packet -> new_auth_protocol_len + 1 ; /* +1 for the \0 */
705+ const size_t remaining_size = packet -> header .size - (size_t ) (p - buf );
706+ const char * const null_terminator = memchr (p , '\0' , remaining_size );
707+ const size_t auth_protocol_len = null_terminator ? (size_t ) (null_terminator - (char * ) p ) : remaining_size ;
708+
709+ packet -> new_auth_protocol = mnd_pestrndup ((char * ) p , auth_protocol_len , FALSE);
710+ packet -> new_auth_protocol_len = auth_protocol_len ;
711+ p += auth_protocol_len ;
712+ if (null_terminator ) {
713+ p ++ ; /* +1 for the \0 */
714+ }
708715
709716 packet -> new_auth_protocol_data_len = packet -> header .size - (size_t ) (p - buf );
710717 if (packet -> new_auth_protocol_data_len ) {
@@ -1950,9 +1957,16 @@ php_mysqlnd_chg_user_read(MYSQLND_CONN_DATA * conn, void * _packet)
19501957 }
19511958 BAIL_IF_NO_MORE_DATA ;
19521959 if (packet -> response_code == 0xFE && packet -> header .size > (size_t ) (p - buf )) {
1953- packet -> new_auth_protocol = mnd_pestrdup ((char * )p , FALSE);
1954- packet -> new_auth_protocol_len = strlen (packet -> new_auth_protocol );
1955- p += packet -> new_auth_protocol_len + 1 ; /* +1 for the \0 */
1960+ const size_t remaining_size = packet -> header .size - (size_t ) (p - buf );
1961+ const char * const null_terminator = memchr (p , '\0' , remaining_size );
1962+ const size_t auth_protocol_len = null_terminator ? (size_t ) (null_terminator - (char * ) p ) : remaining_size ;
1963+
1964+ packet -> new_auth_protocol = mnd_pestrndup ((char * ) p , auth_protocol_len , FALSE);
1965+ packet -> new_auth_protocol_len = auth_protocol_len ;
1966+ p += auth_protocol_len ;
1967+ if (null_terminator ) {
1968+ p ++ ; /* +1 for the \0 */
1969+ }
19561970 packet -> new_auth_protocol_data_len = packet -> header .size - (size_t ) (p - buf );
19571971 if (packet -> new_auth_protocol_data_len ) {
19581972 packet -> new_auth_protocol_data = mnd_emalloc (packet -> new_auth_protocol_data_len );
@@ -2131,9 +2145,16 @@ php_mysqlnd_cached_sha2_result_read(MYSQLND_CONN_DATA * conn, void * _packet)
21312145 if (0xFE == packet -> response_code ) {
21322146 /* Authentication Switch Response */
21332147 if (packet -> header .size > (size_t ) (p - buf )) {
2134- packet -> new_auth_protocol = mnd_pestrdup ((char * )p , FALSE);
2135- packet -> new_auth_protocol_len = strlen (packet -> new_auth_protocol );
2136- p += packet -> new_auth_protocol_len + 1 ; /* +1 for the \0 */
2148+ const size_t remaining_size = packet -> header .size - (size_t ) (p - buf );
2149+ const char * const null_terminator = memchr (p , '\0' , remaining_size );
2150+ const size_t auth_protocol_len = null_terminator ? (size_t ) (null_terminator - (char * ) p ) : remaining_size ;
2151+
2152+ packet -> new_auth_protocol = mnd_pestrndup ((char * ) p , auth_protocol_len , FALSE);
2153+ packet -> new_auth_protocol_len = auth_protocol_len ;
2154+ p += auth_protocol_len ;
2155+ if (null_terminator ) {
2156+ p ++ ; /* +1 for the \0 */
2157+ }
21372158
21382159 packet -> new_auth_protocol_data_len = packet -> header .size - (size_t ) (p - buf );
21392160 if (packet -> new_auth_protocol_data_len ) {
0 commit comments