RobertoMalatesta / miniz

Automatically exported from code.google.com/p/miniz
0 stars 0 forks source link

clang 3.1 with -std=c11 fails to compile miniz.c with narrowing conversions error #6

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
See 
http://stackoverflow.com/questions/4434140/narrowing-conversions-in-c0x-is-it-ju
st-me-or-does-this-sound-like-a-breakin

It seems to me this might actually be a compiler bug because clang thinks 
"\0\0\04\02\06"[num_chans] isn't a constant expression. However, this fixes the 
problem:

@@ -2765,6 +2765,7 @@ mz_uint tdefl_create_comp_flags_from_zip_params(int 
level, int window_bits, int
 // http://altdevblogaday.org/2011/04/06/a-smaller-jpg-encoder/.
 void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h, int num_chans, size_t *pLen_out)
 {
+  static const mz_uint8 chans[] = {0x00, 0x00, 0x04, 0x02, 0x06};
   tdefl_compressor *pComp = (tdefl_compressor *)MZ_MALLOC(sizeof(tdefl_compressor)); tdefl_output_buffer out_buf; int i, bpl = w * num_chans, y, z; mz_uint32 c; *pLen_out = 0;
   if (!pComp) return NULL;
   MZ_CLEAR_OBJ(out_buf); out_buf.m_expandable = MZ_TRUE; out_buf.m_capacity = 57+MZ_MAX(64, (1+bpl)*h); if (NULL == (out_buf.m_pBuf = (mz_uint8*)MZ_MALLOC(out_buf.m_capacity))) { MZ_FREE(pComp); return NULL; }
@@ -2778,7 +2779,7 @@ void *tdefl_write_image_to_png_file_in_memory(const void 
*pImage, int w, int h,
   *pLen_out = out_buf.m_size-41;
   {
     mz_uint8 pnghdr[41]={0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
-      
0,0,(mz_uint8)(w>>8),(mz_uint8)w,0,0,(mz_uint8)(h>>8),(mz_uint8)h,8,"\0\0\04\02\
06"[num_chans],0,0,0,0,0,0,0,
+      
0,0,(mz_uint8)(w>>8),(mz_uint8)w,0,0,(mz_uint8)(h>>8),(mz_uint8)h,8,chans[num_ch
ans],0,0,0,0,0,0,0,
       (mz_uint8)(*pLen_out>>24),(mz_uint8)(*pLen_out>>16),(mz_uint8)(*pLen_out>>8),(mz_uint8)*pLen_out,0x49,0x44,0x41,0x54};
     c=(mz_uint32)mz_crc32(MZ_CRC32_INIT,pnghdr+12,17); for (i=0; i<4; ++i, c<<=8) ((mz_uint8*)(pnghdr+29))[i]=(mz_uint8)(c>>24);
     memcpy(out_buf.m_pBuf, pnghdr, 41);

Original issue reported on code.google.com by toffale...@gmail.com on 22 Jul 2012 at 7:31

GoogleCodeExporter commented 8 years ago
g++ compiles but issues the following warning:

...
miniz.c: In function ‘void* tdefl_write_image_to_png_file_in_memory(const 
void*, int, int, int, size_t*)’:
miniz.c:2781:100: warning: narrowing conversion of 
‘"\000\000\004\002\006"[num_chans]’ from ‘const char’ to ‘mz_uint8 
{aka unsigned char}’ inside { } [-Wnarrowing]
       0,0,(mz_uint8)(w>>8),(mz_uint8)w,0,0,(mz_uint8)(h>>8),(mz_uint8)h,8,"\0\0\04\02\06"[num_chans],0,0,0,0,0,0,0,
                                                                                                    ^
....

using

$ g++ --version
g++ (GCC) 4.8.0 20130411 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Original comment by hdeancl...@gmail.com on 25 May 2013 at 9:38

GoogleCodeExporter commented 8 years ago
This should hopefully be fixed in v1.15. I have only compiled with clang v3.3 
here, and not with -std=c11 though, but I did change this particular code in a 
way that should avoid this problem.

Original comment by richge...@gmail.com on 13 Oct 2013 at 5:24