pprint throws exception on very specific input

Description

I have observed an input-dependent bug in the pprint function of clojure-clr. The bug is triggered in my clojure-clr version 1.5 and 1.7 on the next, fairly minimal example.

user=> (pprint [ ["spec"] ["naamZorgverlener"] ["AGBZorg"] ["email"] ["normalized" :normInstelling]])
[["spec"]
["naamZorgverlener"]
user=> NullReferenceException Object reference not set to an instance of an obj
ect. clojure.lang.Ref.Equals (d:\work\clojure-clr\Clojure\Clojure\Lib\Ref.cs:70
1)

Small changes to the data, make the error disappear, for example

1. Increase the length of the string "spec" to "spec_12345" ("spec_1234" still produces an NullReference exception)

user=> (pprint [ ["spec_12345"] ["naamZorgverlener"] ["AGBZorg"] ["email"] ["normalized" :normInstelling]])
[["spec_12345"]
["naamZorgverlener"]
["AGBZorg"]
["email"]
["normalized" :normInstelling]]
nil

2. removing the element ["email"]

user=> (pprint [ ["spec_12345"] ["naamZorgverlener"] ["AGBZorg"] ["normalized" :normInstelling]])
[["spec_12345"]
["naamZorgverlener"]
["AGBZorg"]
["normalized" :normInstelling]]
nil

I was not able to reproduce this pprint-bug in clojure 1.7 for java.

Environment

clojure-clr 1.5 and 1.7

Activity

Show:
David Miller
November 27, 2015, 5:09 PM

Fix to Ref.CompareTo resolves the error.
Commit 07527db, 2015.11.27

David Miller
November 27, 2015, 4:47 PM

Jeremy:

Per the Clojure contribution rules, I can accept patches only from those who have signed a Contributor Agreement for Clojure. See http://clojure.org/contributing .

In this case, I think I can figure out a fix based on your observations.

-David

Jeremy Sellars
November 22, 2015, 12:30 AM

The exception comes from clojure.lang.Ref.Equals when comparing a ref to null. There are two problems in the equality checking and comparison for Refs. (IComparable.CompareTo is supposed to support CompareTo(null), according to https://msdn.microsoft.com/en-us/library/system.icomparable.compareto(v=vs.110).aspx). That seems to be the root problem. The other affected methods delegated to CompareTo even when comparing against null, which was probably unintentional. The use of ReferenceEquals clarifies the intent.

I am attaching a patch with C# tests. This worked (built on Windows, .net 3.5 & 4.0). I did not test Mono or Linux, although this did not seem to be a platform-specific problem.

I did not look into the pprint code because the C# changes fixed the problem.

(I signed to contributor agreement today before submitting the patch.)

Completed

Assignee

David Miller

Reporter

Cees van Kemenade