commit 1eff1e8214568e68dcd97b71adcdb0626d77407d
Author:     FRIGN <[email protected]>
AuthorDate: Mon Oct 26 12:29:02 2015 +0100
Commit:     sin <[email protected]>
CommitDate: Mon Oct 26 11:55:41 2015 +0000

    Properly handle partial chunks in od(1)
    
    Grab the remaining bytes and fill them up with zeroes in a
    temporary buffer.

diff --git a/od.c b/od.c
index 0bd8f6c..0c361e2 100644
--- a/od.c
+++ b/od.c
@@ -93,6 +93,7 @@ printline(unsigned char *line, size_t len, off_t addr)
        struct type *t = NULL;
        size_t i;
        int first = 1;
+       unsigned char *tmp;
 
        if (TAILQ_EMPTY(&head))
                goto once;
@@ -104,10 +105,17 @@ once:
                } else {
                        printf("%*c", (addr_format == 'n') ? 1 : 7, ' ');
                }
-               for (i = 0; i < len; ) {
-                       printchunk(line + i, t ? t->format : 'o',
-                                  MIN(len - i, t ? t->len : 4));
-                       i += MIN(len - i, t ? t->len : 4);
+               for (i = 0; i < len; i += MIN(len - i, t ? t->len : 4)) {
+                       if (len - i < (t ? t->len : 4)) {
+                               tmp = ecalloc(t ? t->len : 4, 1);
+                               memcpy(tmp, line + i, len - i);
+                               printchunk(tmp, t ? t->format : 'o',
+                                          t ? t->len : 4);
+                               free(tmp);
+                       } else {
+                               printchunk(line + i, t ? t->format : 'o',
+                                          t ? t->len : 4);
+                       }
                }
                fputc('\n', stdout);
                if (TAILQ_EMPTY(&head) || (!len && !first))

Reply via email to