@@ -635,17 +635,40 @@ command_result get_network_registration_state(CommandableIf *t, int &state)
635635 }
636636
637637 constexpr std::string_view pattern = " +CEREG: " ;
638- int state_pos_start;
639- int state_pos_end;
640- if (out.find (pattern) == std::string::npos || (state_pos_start = out.find (' ,' )) == std::string::npos) {
638+
639+ // Check if pattern exists and find its position
640+ size_t pattern_pos = out.find (pattern);
641+ if (pattern_pos == std::string::npos) {
641642 return command_result::FAIL;
642643 }
643644
644- if (out.find (pattern) == std::string::npos || (state_pos_end = out.find (' ,' , state_pos_start)) == std::string::npos) {
645- if (std::from_chars (out.data () + state_pos_start, out.data () + out.size (), state).ec == std::errc::invalid_argument) {
646- return command_result::FAIL;
645+ // Find the first comma after the pattern
646+ size_t state_pos_start = out.find (' ,' , pattern_pos);
647+ if (state_pos_start == std::string::npos) {
648+ return command_result::FAIL;
649+ }
650+
651+ // Find the end of the state value - either a second comma or end of line
652+ size_t state_pos_end = out.find (' ,' , state_pos_start + 1 );
653+ if (state_pos_end == std::string::npos) {
654+ // No second comma found, look for end of line characters
655+ state_pos_end = out.find (' \r ' , state_pos_start);
656+ if (state_pos_end == std::string::npos) {
657+ state_pos_end = out.find (' \n ' , state_pos_start);
658+ }
659+ if (state_pos_end == std::string::npos) {
660+ // No end delimiter found, use end of string
661+ state_pos_end = out.size ();
647662 }
648- } else if (std::from_chars (out.data () + state_pos_start, out.data () + state_pos_end, state).ec == std::errc::invalid_argument) {
663+ }
664+
665+ // Validate that we have a valid range to parse
666+ if (state_pos_start + 1 >= state_pos_end) {
667+ return command_result::FAIL;
668+ }
669+
670+ // Extract state value (skip the comma)
671+ if (std::from_chars (out.data () + state_pos_start + 1 , out.data () + state_pos_end, state).ec == std::errc::invalid_argument) {
649672 return command_result::FAIL;
650673 }
651674
0 commit comments