@@ -116,33 +116,63 @@ func (r *Reader) PeekPushNotificationName() (string, error) {
116116 if buf [0 ] != RespPush {
117117 return "" , fmt .Errorf ("redis: can't parse push notification: %q" , buf )
118118 }
119- // remove push notification type and length
120- buf = buf [2 :]
119+
120+ if len (buf ) < 3 {
121+ return "" , fmt .Errorf ("redis: can't parse push notification: %q" , buf )
122+ }
123+
124+ // remove push notification type
125+ buf = buf [1 :]
126+ // remove first line - e.g. >2\r\n
121127 for i := 0 ; i < len (buf )- 1 ; i ++ {
122128 if buf [i ] == '\r' && buf [i + 1 ] == '\n' {
123129 buf = buf [i + 2 :]
124130 break
131+ } else {
132+ if buf [i ] < '0' || buf [i ] > '9' {
133+ return "" , fmt .Errorf ("redis: can't parse push notification: %q" , buf )
134+ }
125135 }
126136 }
137+ if len (buf ) < 2 {
138+ return "" , fmt .Errorf ("redis: can't parse push notification: %q" , buf )
139+ }
140+ // next line should be $<length><string>\r\n or +<length><string>\r\n
127141 // should have the type of the push notification name and it's length
128- if buf [0 ] != RespString {
142+ if buf [0 ] != RespString && buf [ 0 ] != RespStatus {
129143 return "" , fmt .Errorf ("redis: can't parse push notification name: %q" , buf )
130144 }
131- // skip the length of the string
132- for i := 0 ; i < len (buf )- 1 ; i ++ {
133- if buf [i ] == '\r' && buf [i + 1 ] == '\n' {
134- buf = buf [i + 2 :]
135- break
145+ typeOfName := buf [0 ]
146+ // remove the type of the push notification name
147+ buf = buf [1 :]
148+ if typeOfName == RespString {
149+ // remove the length of the string
150+ if len (buf ) < 2 {
151+ return "" , fmt .Errorf ("redis: can't parse push notification name: %q" , buf )
152+ }
153+ for i := 0 ; i < len (buf )- 1 ; i ++ {
154+ if buf [i ] == '\r' && buf [i + 1 ] == '\n' {
155+ buf = buf [i + 2 :]
156+ break
157+ } else {
158+ if buf [i ] < '0' || buf [i ] > '9' {
159+ return "" , fmt .Errorf ("redis: can't parse push notification name: %q" , buf )
160+ }
161+ }
136162 }
137163 }
138164
165+ if len (buf ) < 2 {
166+ return "" , fmt .Errorf ("redis: can't parse push notification name: %q" , buf )
167+ }
139168 // keep only the notification name
140169 for i := 0 ; i < len (buf )- 1 ; i ++ {
141170 if buf [i ] == '\r' && buf [i + 1 ] == '\n' {
142171 buf = buf [:i ]
143172 break
144173 }
145174 }
175+
146176 return util .BytesToString (buf ), nil
147177}
148178
0 commit comments