about summary refs log tree commit diff
path: root/converter
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2020-06-07 20:14:42 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2020-06-07 20:14:42 +0000
commitec46d9756038dfad5cb0b4db58bece107aa8fb47 (patch)
tree0cdf1d217bc3e2da6a593427103a67b49afc2b2b /converter
parentf9fd97fee9930b2d750b83dc0798934c44031b7e (diff)
downloadnetpbm-mirror-ec46d9756038dfad5cb0b4db58bece107aa8fb47.tar.gz
netpbm-mirror-ec46d9756038dfad5cb0b4db58bece107aa8fb47.tar.xz
netpbm-mirror-ec46d9756038dfad5cb0b4db58bece107aa8fb47.zip
Fix undefined behavior: swab with source and destination the same
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3847 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter')
-rw-r--r--converter/other/cameratopam/util.c22
1 files changed, 18 insertions, 4 deletions
diff --git a/converter/other/cameratopam/util.c b/converter/other/cameratopam/util.c
index 8781473a..ede5ef69 100644
--- a/converter/other/cameratopam/util.c
+++ b/converter/other/cameratopam/util.c
@@ -1,7 +1,9 @@
 #define _XOPEN_SOURCE   /* Make sure unistd.h contains swab() */
 #include <unistd.h>
 
-#include "pm.h"
+#include "netpbm/pm.h"
+#include "netpbm/mallocvar.h"
+
 #include "global_variables.h"
 #include "util.h"
 #include "stdio_nofail.h"
@@ -49,9 +51,21 @@ get4(FILE * const ifp)
 void
 read_shorts (FILE * const ifp, unsigned short *pixel, int count)
 {
-    fread_nofail (pixel, 2, count, ifp);
-    if ((order == 0x4949) == (BYTE_ORDER == BIG_ENDIAN))
-        swab (pixel, pixel, count*2);
+    unsigned short * buffer;
+
+    MALLOCARRAY(buffer, count);
+
+    if (!buffer)
+        pm_error("Failed to allocate a buffer for reading %u short "
+                 "integers from file", count);
+    else {
+        fread_nofail(buffer, 2, count, ifp);
+
+        if ((order == 0x4949) == (BYTE_ORDER == BIG_ENDIAN))
+            swab(buffer, pixel, count*2);
+
+        free(buffer);
+    }
 }
 
 /*