This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug malloc/20137] aligned_alloc should fail for bad size / alignment [DR#460]


https://sourceware.org/bugzilla/show_bug.cgi?id=20137

--- Comment #3 from Florian Weimer <fweimer at redhat dot com> ---
To be clear, the use case I'm thinking about is allocating storage for an
object of this type:

  struct flexible
  {
     _Decimal128 value;
     char formatted[];
  };

Doing something like this (with error checking) looks reasonable to me:

  size_t length = strlen (formatted);
  struct flexible *p = aligned_alloc (_Alignof (struct flexible),
                                      offsetof (struct flexible, formatted)
                                      + length + 1);
  p->value = value;
  memcpy (p->formatted, formatted, length + 1);

I'm surprised this isn't recognized as a useful coding pattern.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]