xbps-checkvers: rewrite check_reverts without malloc
This commit is contained in:
		
				
					committed by
					
						
						Duncan Overbruck
					
				
			
			
				
	
			
			
			
						parent
						
							e495f84954
						
					
				
				
					commit
					49cc70de9a
				
			@@ -479,38 +479,23 @@ rcv_set_distdir(rcv_t *rcv, const char *distdir)
 | 
			
		||||
static bool
 | 
			
		||||
check_reverts(const char *repover, const char *reverts)
 | 
			
		||||
{
 | 
			
		||||
	bool rv = false;
 | 
			
		||||
	char *sreverts, *p;
 | 
			
		||||
	const char *s, *e;
 | 
			
		||||
	size_t len;
 | 
			
		||||
 | 
			
		||||
	assert(reverts);
 | 
			
		||||
	if (!(len = strlen(reverts)))
 | 
			
		||||
 | 
			
		||||
	s = reverts;
 | 
			
		||||
	if ((len = strlen(s)) == 0)
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	assert((sreverts = strdup(reverts)));
 | 
			
		||||
 | 
			
		||||
	for (p = sreverts; (p = strstr(p, repover));) {
 | 
			
		||||
		/*
 | 
			
		||||
		 * Check if it's the first character or the previous character is a
 | 
			
		||||
		 * whitespace.
 | 
			
		||||
		 */
 | 
			
		||||
		if (p > sreverts && !isalpha(p[-1]) && !isspace(p[-1])) {
 | 
			
		||||
			p++; // always advance
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		p += strlen(repover);
 | 
			
		||||
		/*
 | 
			
		||||
		 * Check if it's the last character or if the next character is a
 | 
			
		||||
		 * whitespace
 | 
			
		||||
		 */
 | 
			
		||||
		if (isspace(*p) || *p == '\0') {
 | 
			
		||||
			rv = true;
 | 
			
		||||
			break;
 | 
			
		||||
		}
 | 
			
		||||
	for (s = reverts; s < reverts+len; s = e+1) {
 | 
			
		||||
		if (!(e = strchr(s, ' ')))
 | 
			
		||||
			e = reverts+len;
 | 
			
		||||
		if (strncmp(s, repover, e-s) == 0)
 | 
			
		||||
			return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	free(sreverts);
 | 
			
		||||
	return rv;
 | 
			
		||||
	return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
 
 | 
			
		||||
@@ -381,7 +381,62 @@ EOF
 | 
			
		||||
	xbps-checkvers -R $PWD/some_repo -D $PWD/void-packages
 | 
			
		||||
	out=$(xbps-checkvers -R $PWD/some_repo -D $PWD/void-packages)
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	atf_check_equal "$out" "fs-utils 1.10_1 1.10_1"
 | 
			
		||||
	atf_check_equal "$out" ""
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
atf_test_case reverts_many
 | 
			
		||||
 | 
			
		||||
reverts_many_head() {
 | 
			
		||||
	atf_set "descr" "xbps-checkvers(1): test with multiple reverts"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
reverts_many_body() {
 | 
			
		||||
	mkdir -p some_repo pkg_A void-packages/srcpkgs/A
 | 
			
		||||
	touch pkg_A/file00
 | 
			
		||||
	cat > void-packages/srcpkgs/A/template <<EOF
 | 
			
		||||
pkgname=A
 | 
			
		||||
reverts="1.1_1 1.2_1 1.3_1 1.3_2 1.3_3 1.3_4"
 | 
			
		||||
version=1.0
 | 
			
		||||
revision=1
 | 
			
		||||
do_install() {
 | 
			
		||||
	:
 | 
			
		||||
}
 | 
			
		||||
EOF
 | 
			
		||||
	cd some_repo
 | 
			
		||||
	xbps-create -A noarch -n A-1.2_1 -s "A pkg" ../pkg_A
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	xbps-rindex -d -a $PWD/*.xbps
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	cd ..
 | 
			
		||||
	out=$(xbps-checkvers -R $PWD/some_repo -D $PWD/void-packages)
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	atf_check_equal "$out" "A 1.2_1 1.0_1"
 | 
			
		||||
 | 
			
		||||
	cd some_repo
 | 
			
		||||
	rm *.xbps
 | 
			
		||||
	xbps-rindex -c .
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	xbps-create -A noarch -n A-1.1_1 -s "A pkg" ../pkg_A
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	xbps-rindex -d -a $PWD/*.xbps
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	cd ..
 | 
			
		||||
	out=$(xbps-checkvers -R $PWD/some_repo -D $PWD/void-packages)
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	atf_check_equal "$out" "A 1.1_1 1.0_1"
 | 
			
		||||
 | 
			
		||||
	cd some_repo
 | 
			
		||||
	rm *.xbps
 | 
			
		||||
	xbps-rindex -c .
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	xbps-create -A noarch -n A-1.3_4 -s "A pkg" ../pkg_A
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	xbps-rindex -d -a $PWD/*.xbps
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	cd ..
 | 
			
		||||
	out=$(xbps-checkvers -R $PWD/some_repo -D $PWD/void-packages)
 | 
			
		||||
	atf_check_equal $? 0
 | 
			
		||||
	atf_check_equal "$out" "A 1.3_4 1.0_1"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
atf_test_case manual_mode
 | 
			
		||||
@@ -422,5 +477,6 @@ atf_init_test_cases() {
 | 
			
		||||
	atf_add_test_case srcpkg_with_a_ref_and_comment
 | 
			
		||||
	atf_add_test_case reverts
 | 
			
		||||
	atf_add_test_case reverts_alpha
 | 
			
		||||
	atf_add_test_case reverts_many
 | 
			
		||||
	atf_add_test_case manual_mode
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user