Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure nothing is nil in tmp slice #1423

Merged
merged 1 commit into from
Nov 14, 2022

Conversation

thor-son
Copy link
Contributor

@thor-son thor-son commented Nov 8, 2022

Annotation is writed Make sure nothing is nil.
when dst slice cap is bigger than len, some argsKV elements is nil.
So fill empty slice in for loop and Make sure nothing is nil.

@li-jin-gou
Copy link
Contributor

Some code examples and descriptions can be added to help understand the role of PR

@thor-son
Copy link
Contributor Author

thor-son commented Nov 9, 2022

use start index len(dst) for loop (current)

	tmp := make([]argsKV, 5)
	dst := make([]argsKV, 2)
	dst[0] = argsKV{key: []byte{'k'}, value: []byte{'0'}}
	dst[1] = argsKV{key: []byte{'k'}, value: []byte{'1'}}

	dst = append(dst, argsKV{key: []byte{'k'}, value: []byte{'2'}})
	//cap(dst) == 4, len(dst) == 3
	dst = dst[:cap(dst)]
	//cap(dst) == 4, len(dst) == 4
	copy(tmp, dst)

	for i := len(dst); i < len(tmp); i++ {
		// Make sure nothing is nil.
		tmp[i].key = []byte{}
		tmp[i].value = []byte{}
	}
	fmt.Println(tmp[3].key == nil) // true
	fmt.Println(tmp[4].key == nil) // false

use start index dstLen for loop (pr)

	tmp := make([]argsKV, 5)
	dst := make([]argsKV, 2)
	dst[0] = argsKV{key: []byte{'k'}, value: []byte{'0'}}
	dst[1] = argsKV{key: []byte{'k'}, value: []byte{'1'}}

	dst = append(dst, argsKV{key: []byte{'k'}, value: []byte{'2'}})
	//cap(dst) == 4, len(dst) == 3
	dstLen := len(dst)
	dst = dst[:cap(dst)]
	//cap(dst) == 4, len(dst) == 4
	copy(tmp, dst)

	for i := dstLen; i < len(tmp); i++ {
		// Make sure nothing is nil.
		tmp[i].key = []byte{}
		tmp[i].value = []byte{}
	}
	fmt.Println(tmp[3].key == nil) // false.  <-- Make sure nothing is nil.
	fmt.Println(tmp[4].key == nil) // false

@erikdubbelboer erikdubbelboer merged commit c57a2ce into valyala:master Nov 14, 2022
@erikdubbelboer
Copy link
Collaborator

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants