Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixes against keys containing special characters

Thanks to Enric Lafont for a bug report
  • Loading branch information...
commit dd5e8579574ee749da77eedb201e67d516893dea 1 parent 0acf676
Krzysztof Kliś authored October 05, 2011
13  memcache.go
@@ -34,7 +34,6 @@ import (
34 34
 	"bufio"
35 35
 	"net"
36 36
 	"os"
37  
-	"regexp"
38 37
 	"strconv"
39 38
 	"strings"
40 39
 )
@@ -81,9 +80,11 @@ func (memc *Memcache) Get(key string) (value []byte, flags int, err os.Error) {
81 80
 	}
82 81
 	reader := bufio.NewReader(memc.conn)
83 82
 	line, err := reader.ReadString('\n')
84  
-	re, _ := regexp.Compile("VALUE " + key + " ([0-9]+) ([0-9]+)")
85  
-	a := re.FindStringSubmatch(line)
86  
-	if len(a) != 3 {
  83
+	if err != nil {
  84
+		return
  85
+	}
  86
+	a := strings.Split(strings.TrimSpace(line), " ")
  87
+	if len(a) != 4 || a[0] != "VALUE" || a[1] != key {
87 88
 		if line == "END\r\n" {
88 89
 			err = NotFoundError
89 90
 		} else {
@@ -91,8 +92,8 @@ func (memc *Memcache) Get(key string) (value []byte, flags int, err os.Error) {
91 92
 		}
92 93
 		return
93 94
 	}
94  
-	flags, _ = strconv.Atoi(a[1])
95  
-	l, _ := strconv.Atoi(a[2])
  95
+	flags, _ = strconv.Atoi(a[2])
  96
+	l, _ := strconv.Atoi(a[3])
96 97
 	value = make([]byte, l)
97 98
 	n := 0
98 99
 	for {
10  memcache_test.go
... ...
@@ -1,5 +1,5 @@
1 1
 package memcache
2  
- 
  2
+
3 3
 import (
4 4
 	"strconv"
5 5
 	"testing"
@@ -15,7 +15,7 @@ func TestClient(t *testing.T) {
15 15
 		t.Error(err.String())
16 16
 	}
17 17
 	// clean
18  
-	memc.Delete("foo")
  18
+	memc.Delete(key)
19 19
 	// test add
20 20
 	err = memc.Add(key, []uint8(value), flags, 0)
21 21
 	if err != nil {
@@ -41,11 +41,11 @@ func TestClient(t *testing.T) {
41 41
 	}
42 42
 	testGet(t, memc, value + value + value)
43 43
 	// test delete
44  
-	err = memc.Delete("foo")
  44
+	err = memc.Delete(key)
45 45
 	if err != nil {
46 46
 		t.Error(err.String())
47 47
 	}
48  
-	_, _, err = memc.Get("foo")
  48
+	_, _, err = memc.Get(key)
49 49
 	if err == nil {
50 50
 		t.Error("Data not removed from memcache")
51 51
 	}
@@ -72,7 +72,7 @@ func TestClient(t *testing.T) {
72 72
 }
73 73
 
74 74
 func testGet(t *testing.T, memc *Memcache, s string) {
75  
-	val, fl, err := memc.Get("foo")
  75
+	val, fl, err := memc.Get(key)
76 76
 	if err != nil {
77 77
 		t.Error(err.String())
78 78
 	}

0 notes on commit dd5e857

Please sign in to comment.
Something went wrong with that request. Please try again.