Skip to content

Mockall will format function arguments too greedily #647

@asomers

Description

@asomers

If a mock method's expectations aren't satisfied, Mockall will print an error message that, if possible, includes every method argument formatted with Debug::fmt. That's been the case with stable rust ever since #425 . But with nightly rust, when using Mockall's nightly feature, it's been the case for much longer.
The problem is that Mockall actually formats the error message before checking if the mock method call would fail, in MockFunction::desc1. That has two problems:

  1. It wastes CPU cycles, potentially lots of them if Debug::fmt is expensive on that object.
  2. It calls Debug::fmt on objects where that shouldn't be allowed. That's a problem for implementers of std::io::Read::read, which by contractgentlemen's agreement aren't supposed to read data from their arguments. If the argument is an uninitialized buffer (a common case), then formatting that argument with Debug::fmt could even technically be UB. And Miri will complain about it.

We should fix this problem by changing MockFunction::desc to return a closure which will produce a format string, rather than the format string itself. And ripple that through the stack accordingly.

Footnotes

  1. https://github.com/asomers/mockall/blob/aeb460c5aca3f8f38b855cd459d718af265b32fb/mockall_derive/src/mock_function.rs#L632

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions