fixed issue #1, user data could contain '\x00' NULL byte now

This commit is contained in:
mikespook 2011-05-28 12:27:27 +08:00
parent 76f61da7ff
commit 28ac570cbc
5 changed files with 17 additions and 7 deletions

View File

@ -9,7 +9,7 @@ func main() {
client := gearman.NewClient() client := gearman.NewClient()
defer client.Close() defer client.Close()
client.AddServer("127.0.0.1:4730") client.AddServer("127.0.0.1:4730")
echo := []byte("Hello world") echo := []byte("Hello\x00world")
if data, err := client.Echo(echo); err != nil { if data, err := client.Echo(echo); err != nil {
log.Println(string(data)) log.Println(string(data))

View File

@ -204,7 +204,7 @@ func (client *Client) Status(handle string) (known, running bool, numerator, den
if job, err = client.readLastJob(STATUS_RES); err != nil { if job, err = client.readLastJob(STATUS_RES); err != nil {
return return
} }
data := splitByteArray(job.Data, '\x00') data := splitByteArray(job.Data, '\x00', 5)
if len(data) != 5 { if len(data) != 5 {
err = os.NewError("Data Error.") err = os.NewError("Data Error.")
return return

View File

@ -65,7 +65,7 @@ func (job *ClientJob) Result() (data []byte, err os.Error) {
err = os.NewError("Work exception.") err = os.NewError("Work exception.")
fallthrough fallthrough
case WORK_COMPLETE: case WORK_COMPLETE:
s := splitByteArray(job.Data, '\x00') s := splitByteArray(job.Data, '\x00', 2)
if len(s) != 2 { if len(s) != 2 {
err = os.NewError("Invalid data.") err = os.NewError("Invalid data.")
return return
@ -84,7 +84,7 @@ func (job *ClientJob) Update() (data []byte, err os.Error) {
err = os.NewError("The job is not a update.") err = os.NewError("The job is not a update.")
return return
} }
s := splitByteArray(job.Data, '\x00') s := splitByteArray(job.Data, '\x00', 2)
if len(s) != 2 { if len(s) != 2 {
err = os.NewError("Invalid data.") err = os.NewError("Invalid data.")
return return

View File

@ -85,13 +85,17 @@ type Job interface {
} }
// Splite the byte array by a byte // Splite the byte array by a byte
func splitByteArray(slice []byte, spot byte) (data [][]byte) { func splitByteArray(slice []byte, spot byte, limit int) (data [][]byte) {
data = make([][]byte, 0, 10) data = make([][]byte, 0, 10)
start, end := 0, 0 start, end := 0, 0
for i, v := range slice { for i, v := range slice {
if v == spot { if v == spot {
if start != end { if start != end {
data = append(data, slice[start:end]) data = append(data, slice[start:end])
if (limit > 0) && (limit - 1 == len(data)) {
start = end
break
}
} }
start, end = i+1, i+1 start, end = i+1, i+1
} else { } else {
@ -104,7 +108,7 @@ func splitByteArray(slice []byte, spot byte) (data [][]byte) {
// Extract the error message // Extract the error message
func getError(data []byte) (eno os.Errno, err os.Error) { func getError(data []byte) (eno os.Errno, err os.Error) {
rel := splitByteArray(data, '\x00') rel := splitByteArray(data, '\x00', 2)
if len(rel) != 2 { if len(rel) != 2 {
err = os.NewError("The input is not a error data.") err = os.NewError("The input is not a error data.")
return return

View File

@ -217,7 +217,13 @@ func (worker *Worker) SetId(id string) (err os.Error) {
// Execute the job. And send back the result. // Execute the job. And send back the result.
func (worker *Worker) exec(job *WorkerJob) (err os.Error) { func (worker *Worker) exec(job *WorkerJob) (err os.Error) {
jobdata := splitByteArray(job.Data, '\x00') var limit int
if job.DataType == JOB_ASSIGN {
limit = 3
} else {
limit = 4
}
jobdata := splitByteArray(job.Data, '\x00', limit)
job.Handle = string(jobdata[0]) job.Handle = string(jobdata[0])
funcname := string(jobdata[1]) funcname := string(jobdata[1])
if job.DataType == JOB_ASSIGN { if job.DataType == JOB_ASSIGN {