Skip to content

Eigen::Vec2 is misaligned but condition cannot check it. #97

@recsater

Description

@recsater

Eigen::Vec3, Eigen:: Mat3 and so on is ok except Eigen::Vec2

Make Eigen formattable

include "eigen3/Eigen/Core"

#define _FMTLOG_EIGEN_FORMAT(_Scalar, _Rows, _Cols)                          \
  template <>                                                                \
  struct fmt::formatter<Eigen::Matrix<_Scalar, _Rows, _Cols>>                \
      : formatter<std::string_view> {                                        \
                                                                             \
    auto format(const Eigen::Matrix<_Scalar, _Rows, _Cols>& c,               \
                format_context& ctx) const {                                 \
      std::stringstream ss;                                                  \
      ss << c.format(Eigen::IOFormat(5, 0, ", ", "\n", "[", "]", "[", "]")); \
      return fmt::formatter<fmt::string_view>::format(ss.str(), ctx);        \
    }                                                                        \
  };

_FMTLOG_EIGEN_FORMAT(double, -1, -1)
_FMTLOG_EIGEN_FORMAT(double, 2, -1)
_FMTLOG_EIGEN_FORMAT(double, 2, 1)
_FMTLOG_EIGEN_FORMAT(double, 2, 2)
_FMTLOG_EIGEN_FORMAT(double, 3, -1)
_FMTLOG_EIGEN_FORMAT(double, 3, 1)
_FMTLOG_EIGEN_FORMAT(double, 3, 2)
_FMTLOG_EIGEN_FORMAT(double, 3, 3)
_FMTLOG_EIGEN_FORMAT(double, 4, -1)
_FMTLOG_EIGEN_FORMAT(double, 4, 1)
_FMTLOG_EIGEN_FORMAT(double, 4, 2)
_FMTLOG_EIGEN_FORMAT(double, 4, 3)
_FMTLOG_EIGEN_FORMAT(double, 4, 4)
logi("{}", Eigen::Mat3); // OK
logi("{}", Eigen::Vec2); // Segfault

Segfault

// fmtlog.h :487
    if constexpr (std::is_trivially_copyable_v<fmt::remove_cvref_t<Arg>>) { // false for Eigen::Vec2 
        memcpy(out, &arg, sizeof(Arg)); // If run this line, formatting is successful.
      }
      else {
        new (out) fmt::remove_cvref_t<Arg>(std::forward<Arg>(arg));
      }
    return encodeArgs<CstringIdx>(cstringSize, out + sizeof(Arg), std::forward<Args>(args)...); // Segfault at this line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions