Odd crash

Q&A's, tips, howto's
Locked
bludra84
Posts: 7
Joined: Thu Aug 27, 2009 1:55 pm

Odd crash

Post by bludra84 »

I have been messing with some code to respond to simple HTTP requests and have run into a weird bug. The following code results in a crash with this error:
*** glibc detected *** /usr/bin/newlisp: double free or corruption (fasttop): 0x

Code: Select all

#!/usr/bin/newlisp

(constant 'max-bytes 1024)

(define (error num) 
    (format "Error: %d" num))

(define (process-request request)
    (set 'request (parse request " "))
    (case (first request)
        ("GET"  (get-request (request 1)))
        ("HEAD" (create-head (request 1)))
        (true   (error 501))))

(define (get-request item)
    (set 'item (replace "../" item ""))
    (if (ends-with item {\.css|\.html} 1) (send-file (1 item))
        (string "file not allowed\n")))

(define (send-file filename)
    (if (file? filename) (read-file filename)
        (error 404)))

(define (create-head item)
    (string "head"))


;
; PROGRAM START
;
(if (not (setf listen (net-listen 8080)))
    (print (net-error)))

(while (not (net-error))
    (setf connection (net-accept listen))
    (net-receive connection message max-bytes)
    (net-send connection (process-request message))
    (close connection))


;(while true
;    (print (process-request "GET /test.html")))

(exit)
I test this by running:

Code: Select all

echo "GET /test.html" | nc localhost 8080
The while loop commented out at the bottom will run fine for me. So all the code should be ok except for the loop where I manage the connection. Have I done something weird there, or is this a bug in newlisp?

bludra84
Posts: 7
Joined: Thu Aug 27, 2009 1:55 pm

Post by bludra84 »

Oh, and this is in linux version 10.1.5. Thank you in advance.

TedWalther
Posts: 608
Joined: Mon Feb 05, 2007 1:04 am
Location: Abbotsford, BC
Contact:

Post by TedWalther »

Did you enable IPv6?

Ted

bludra84
Posts: 7
Joined: Thu Aug 27, 2009 1:55 pm

Post by bludra84 »

No, I am just using IPv4.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

I found a problem in the "ends-with/start-with" function, when using regular expressions.

You could either change the code in your program from:

Code: Select all

    (if (ends-with item {\.css|\.html} 1) (send-file (1 item))
        (string "file not allowed\n")))
to:

Code: Select all

    (if (find {\.html$|\.css$} item 1) (send-file (1 item))
        (string "file not allowed\n")))
or make the following change in nl-liststr.c in function startsEndsWith()

from:

Code: Select all

        {
        free(keydollar);
        if(pos + klen == slen)
            return(trueCell);
        }
to:

Code: Select all

        if(pos + klen == slen)
            {
            free(keydollar);
            return(trueCell);
            }
in the newLISP source code.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

... but the test will still fail in your program because 'item' contains a trailing line-feed.

Code: Select all

(println "=>" item "<=")

Code: Select all

=>/test.html
<=
Last edited by Lutz on Tue Sep 29, 2009 4:43 pm, edited 1 time in total.

bludra84
Posts: 7
Joined: Thu Aug 27, 2009 1:55 pm

Post by bludra84 »

Ahh, thanks for the help! So is this a bug that will be fixed or was I missinterpreting how to use ends-with?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

you have used 'ends-with' correctly.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You could use 'trim' to get rid of the trailing line feed.

bludra84
Posts: 7
Joined: Thu Aug 27, 2009 1:55 pm

Post by bludra84 »

Great, thanks for the tip.

Locked