fix malloc errors in query code