On 06/12/12 14:32, Niall O'Reilly wrote:
> On 6 Dec 2012, at 14:14, Igor Tandetnik wrote:
> 
> > Your code assumes, in several places, that strings passed to collation 
> > function are NUL-terminated. They don't have to be - that's why lengths are 
> > also passed. I think this may be causing the problem you are seeing: when 
> > the string comes from a literal (as in x < '' ) it just may happen to be 
> > NUL-terminated, but when it comes straight from the database, it may not 
> > be, and you are cheerfully reading garbage past the end of buffer.
>
>       Thanks for your analysis and helpful comments.
> 
>       I'll need to take care to make a NUL-terminated copy of each source 
> string,
>       as inet_pton doesn't take a count argument.

        That seems to have done the trick.  Thanks again, Igor.

basement(niall)61: sqlite3 <demo-ip-extension.sql
.version
SQLite 3.7.9 2011-11-01 00:52:41 c7c6050ef060877ebe77b41d959e9df13f8c9b5e
select load_extension('./ip-extension.so');

create table foo (x collate ipaddress);
insert into foo values('::1');
insert into foo values('127.0.0.1');
select rowid, *, NULL from foo;
1|::1|
2|127.0.0.1|
select rowid, *, NULL from foo order by x;
2|127.0.0.1|
1|::1|
insert into foo values('100A');
insert into foo values('128A');
insert into foo values(' ABCD');
insert into foo values('');
select rowid, *, NULL from foo;
1|::1|
2|127.0.0.1|
3|100A|
4|128A|
5| ABCD|
6||
select rowid, *, NULL from foo where x < '' order by x;
2|127.0.0.1|
1|::1|
select rowid, *, NULL from foo where x > '' order by x;
5| ABCD|
3|100A|
4|128A|
select rowid, *, NULL from foo order by x;
2|127.0.0.1|
1|::1|
6||
5| ABCD|
3|100A|
4|128A|
select rowid, *, NULL from foo order by x collate binary;
6||
5| ABCD|
3|100A|
2|127.0.0.1|
4|128A|
1|::1|
basement(niall)61:

        /Niall

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to