I noticed if you call listreceivedbyaddress with true for incude_empty it was skipping the cross-reference against mapTally. The skip causes the entire address book (including “send” purpose) to be included in the results erroneously. There should be a “if purpose not ‘send’ then include results”.
Code would be inserted here:
https://github.com/bitcoin/bitcoin/blob/master/src/wallet/rpcwallet.cpp#L1122
something like:
0 if(item_it->second.purpose == "send")
1 continue;