[rt.cpan.org #74578] Win32::API::Struct alignment wrong

[email protected] ("Douglas Wilson via RT")
Newsgroups perl.libwin32
Message-ID <[email protected]>
Mon Feb 13 15:32:14 2012: Request 74578 was acted upon.
Transaction: Correspondence added by DOUGW
       Queue: Win32-API
     Subject: Win32::API::Struct alignment wrong
   Broken in: 0.64
    Severity: Normal
       Owner: Nobody
  Requestors: [email protected]
      Status: open
 Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=74578 >


On Fri Feb 10 06:43:35 2012, [email protected] wrote:
> 
> I'm going to release this patch to CPAN
> during the weekend.
> 

I see you perltidy'd everything. Here's a patch against version 0.65 in
case you want to just use the 'patch' command.
Struct.pm.diff.txt (text/plain, 2.5 KB)
--- Struct.pm	Sun Feb 12 06:39:26 2012
+++ Struct.pm	Mon Feb 13 12:22:16 2012
@@ -212,9 +212,10 @@
             $packed_size += $subpacksize;
         }
         else {
+            my $repeat = 1;
             if ($type =~ /\w\*(\d+)/) {
-                my $size = $1;
-                $type = "a$size";
+                $repeat = $1;
+                $type = "a$repeat";
             }
 
             DEBUG "(PM)Struct::getPack($self->{__typedef__}) ++ $type\n";
@@ -230,7 +231,7 @@
             $type_size  = Win32::API::Type::sizeof($orig);
             $type_align = (($packed_size + $type_size) % $type_size);
             $packing .= "x" x $type_align . $type;
-            $packed_size += $type_size + $type_align;
+            $packed_size += ( $type_size * $repeat ) + $type_align;
         }
     }
 
@@ -267,7 +268,7 @@
     foreach my $member (@{$self->{typedef}}) {
         ($name, $type, $orig) = @$member;
         if ($type eq '>') {
-            my ($subpacking, @subitems, $subpacksize) = $self->{$name}->getUnpack();
+            my ($subpacking, $subpacksize, @subitems) = $self->{$name}->getUnpack();
             DEBUG "(PM)Struct::getUnpack($self->{__typedef__}) ++ $subpacking\n";
             $packing .= $subpacking;
             $packed_size += $subpacksize;
@@ -274,26 +275,27 @@
             push(@items, @subitems);
         }
         else {
+            my $repeat = 1;
             if ($type =~ /\w\*(\d+)/) {
-                my $size = $1;
-                $type = "Z$size";
+                $repeat = $1;
+                $type = "Z$repeat";
             }
             DEBUG "(PM)Struct::getUnpack($self->{__typedef__}) ++ $type\n";
             $type_size  = Win32::API::Type::sizeof($orig);
             $type_align = (($packed_size + $type_size) % $type_size);
             $packing .= "x" x $type_align . $type;
-            $packed_size += $type_size + $type_align;
+            $packed_size += ( $type_size * $repeat ) + $type_align;
 
             push(@items, $name);
         }
     }
     DEBUG "(PM)Struct::getUnpack($self->{__typedef__}): unpack($packing, @items)\n";
-    return ($packing, @items, $packed_size);
+    return ($packing, $packed_size, @items);
 }
 
 sub Unpack {
     my $self = shift;
-    my ($packing, @items) = $self->getUnpack();
+    my ($packing, undef, @items) = $self->getUnpack();
     my @itemvalue = unpack($packing, $self->{buffer});
     DEBUG "(PM)Struct::Unpack: unpack($packing, buffer) = @itemvalue\n";
     foreach my $i (0 .. $#items) {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.