read-only string inspection (pointer-arithmetic based)

Jul 15, 2010 at 11:52 AM
Edited Jul 15, 2010 at 12:24 PM

Hello all,

the following code is (slightly modified header inclusion for convenience) derived from strnlen from FreeBSD

/*-
 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/* 
 * derived from (modified!)
 * http://www.freebsd.org/cgi/cvsweb.cgi/src/lib/libc/string/strnlen.c?rev=1.1.2.1.4.1;content-type=text%2Fplain
 */

#include "vcc.h"
#include "limits.h"

typedef unsigned int size_t;

size_t strnlen(const char *s, size_t maxlen)
{
	size_t len;

for (len = 0; len < maxlen; len++, s++) 
   {
      if (!*s)
      break;
   }
return (len);
}
I can verify something close (but no pointer arithmetic) to this as: 
/* BSD copyright headers see above in this post! (I guess they imply 
that also the code annotations are BSD license) */

#include "vcc.h"
#include "limits.h"

typedef unsigned int size_t;

size_t strnlen(const char *s, size_t maxlen)
requires(thread_local(as_array(s, maxlen)))
ensures(forall (unsigned int i; i < result ==> s[i] != 0))
ensures(result <= maxlen)
ensures(result < maxlen ==> s[result] == 0)
{
    size_t len;
    for (len = 0; len < maxlen; len++) 
    invariant(len >= 0 && len <= maxlen)
    invariant(forall (unsigned int i; i < len ==> s[i] != 0))
    {
       if (!s[len])
          break;
    }
    return (len);
}
Question: can I also retain the if(!*s) instead of the if(!s[len]) construct and the 
s++ in the for loop?  I could think of some memory reinterpretation but this 
would demand writeability and leave a lot of root pointer set "space debris" ...
Cheers, Holger
Coordinator
Jul 15, 2010 at 2:47 PM

It should also work with pointer arithmetic. You certainly don’t need any reinterpretation here. You may need to save the initial s pointer into a separate (ghost) variable, something like:

char *s0 = s;

...

invariant(forall(... s0 ...))

invariant(s == &s0[len])

Michal

Jul 15, 2010 at 3:36 PM

Hi Michal, thanks works out of the box (as follow-up worked example posted below) ...

  #include "vcc.h"
  /* this is BSD code, license see above in this thread */
size_t strnlen(const char *s, size_t maxlen)
requires(thread_local(as_array(s, maxlen)))
ensures(forall (unsigned int i; i < result ==> s[i] != 0))
ensures(result <= maxlen)
ensures(result < maxlen ==> s[result] == 0)
{
    size_t len;
    speconly ( char *s0 = s; )

    for (len = 0; len < maxlen; len++, s++) 
    invariant(len >= 0 && len <= maxlen)
    invariant(s == &s0[len])
    invariant(forall (unsigned int i; i < len ==> s0[i] != 0))
    {
		if (!*s)
			break;
     }
     return (len);
}