In gmane.os.openbsd.ports, you wrote:
> On Mon, Mar 12, 2012 at 12:56:36PM +0000, Olivier Mehani wrote:
>> However, there is some possibly dirty things happening as *t is a
>> pointer equals to trim_end's argument const char *str. Could this be
>> some protection forbidding functions to modifiy anything in the memory
>> passed as const?
> /etc/malloc.conf ?

Right. I wasn't aware of that. Thanks for the pointer.  However, nothing
special is set there (neither the symlink nor the environment variable).

I confirmed that the problem seems to be due to the const manipulation
addness, as the following test program segfaults in the same way.

int test (const char* a) {
        char *t=a;
        t[0]='a';
        return 0;
}

int main() {
        test("bbba");
        return 0;
}

GCC also complains when this get compiled but, oddly enough, not the
port when compiling shared.c...

>> Did anybody notice anything similar? Any idea on how to fix it or
>> investigate it further?
> Never had any issue on amd64 since i've imported it. You may discuss it
> with upstream... or try the new versions (0.9.0.2 is in current)

Yeah, the code for that function actually changed to something cleaner [0,1].
I'm not too keen on upgrading this server to current, but backporting the patch
[0] with the following as patches/patch-shared_c seems to fix the problem.

$OpenBSD$
--- shared.c.orig       Sat Mar  5 13:52:39 2011
+++ shared.c    Wed Mar 14 05:12:25 2012
@@ -98,23 +98,17 @@ void *cgit_free_commitinfo(struct commitinfo *info)
 char *trim_end(const char *str, char c)
 {
        int len;
-       char *s, *t;
 
        if (str == NULL)
                return NULL;
-       t = (char *)str;
-       len = strlen(t);
-       while(len > 0 && t[len - 1] == c)
                len--;
-
+       len = strlen(str);
+       while(len > 0 && str[len - 1] == c)
+               len--;
        if (len == 0)
                return NULL;
 
-       c = t[len];
-       t[len] = '\0';
-       s = xstrdup(t);
-       t[len] = c;
-       return s;
+       return strndup(str, len);
 }
 
 char *strlpart(char *txt, int maxlen)


Thanks for your help!

[0] http://hjemli.net/git/cgit/diff/shared.c?id=v0.8.3.5&id2=master
[1] http://hjemli.net/git/cgit/tree/shared.c?id=master#n102

-- 
Olivier Mehani <[email protected]>
PGP fingerprint: 4435 CF6A 7C8D DD9B E2DE  F5F9 F012 A6E2 98C6 6655

Reply via email to