fhanau / Efficient-Compression-Tool

Fast and effective C++ file optimizer
Apache License 2.0
595 stars 41 forks source link

Use getcwd instead of getwd #39

Closed ghuls closed 7 years ago

ghuls commented 7 years ago

Use getcwd instead of getwd:

leanify/zip.cpp: In member function ‘uint32_t Zip::RecompressFile(unsigned char*, uint32_t, uint32_t, std::__cxx11::string, const ECTOptions&)’:
leanify/zip.cpp:188:14: warning: ‘char* getwd(char*)’ is deprecated [-Wdeprecated-declarations]
   char* t0 = getwd(0);
              ^
In file included from /usr/include/features.h:367:0,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h:482,
                 from /usr/include/c++/5/cstdio:41,
                 from leanify/../main.h:9,
                 from leanify/zip.h:4,
                 from leanify/zip.cpp:1:
/usr/include/x86_64-linux-gnu/bits/unistd.h:221:1: note: declared here
 __NTH (getwd (char *__buf))
 ^
leanify/zip.cpp:188:14: warning: ‘char* getwd(char*)’ is deprecated [-Wdeprecated-declarations]
   char* t0 = getwd(0);
              ^
In file included from /usr/include/features.h:367:0,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h:482,
                 from /usr/include/c++/5/cstdio:41,
                 from leanify/../main.h:9,
                 from leanify/zip.h:4,
                 from leanify/zip.cpp:1:
/usr/include/x86_64-linux-gnu/bits/unistd.h:221:1: note: declared here
 __NTH (getwd (char *__buf))
 ^
leanify/zip.cpp:188:21: warning: null argument where non-null required (argument 1) [-Wnonnull]
   char* t0 = getwd(0);
                     ^
leanify/zip.cpp:188:21: warning: ‘char* getwd(char*)’ is deprecated [-Wdeprecated-declarations]
In file included from /usr/include/features.h:367:0,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/5/bits/c++config.h:482,
                 from /usr/include/c++/5/cstdio:41,
                 from leanify/../main.h:9,
                 from leanify/zip.h:4,
                 from leanify/zip.cpp:1:
/usr/include/x86_64-linux-gnu/bits/unistd.h:221:1: note: declared here
 __NTH (getwd (char *__buf))
 ^
leanify/zip.cpp:212:54: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
     fread(data - size_leanified, 1, new_size, stream);
                                                      ^
In file included from /usr/include/unistd.h:1151:0,
                 from leanify/zip.cpp:7:
In function ‘char* getwd(char*)’,
    inlined from ‘uint32_t Zip::RecompressFile(unsigned char*, uint32_t, uint32_t, std::__cxx11::string, const ECTOptions&)’ at leanify/zip.cpp:188:21:
/usr/include/x86_64-linux-gnu/bits/unistd.h:225:29: warning: call to ‘__getwd_warn’ declared with attribute warning: please use getcwd instead, as getwd doesn't specify buffer size
   return __getwd_warn (__buf);
                             ^
/tmp/ccUoq7Wg.o: In function `Zip::RecompressFile(unsigned char*, unsigned int, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, ECTOptions const&)':
zip.cpp:(.text+0xb33): warning: the `getwd' function is dangerous and should not be used.
fhanau commented 7 years ago

Does adf32f3 fix all warnings?

ghuls commented 7 years ago

It fixes the getwd related warnings.

This is the only warning I get now for zip.cpp:

zopfli/blocksplitter.c optipng/opngreduc/opngreduc.c LzFind.c miniz/miniz.c
g++ -pthread -Ofast -std=gnu++11 -fsigned-char main.cpp blocksplitter.o codec.o image.o lz77.o opngreduc.o squeeze.o util.o LzFind.o miniz.o support.cpp zopflipng.cpp zopfli/deflate.cpp zopfli/zopfli_gzip.cpp zopfli/katajainen.cpp lodepng/lodepng.cpp lodepng/lodepng_util.cpp optipng/optipng.cpp jpegtran.cpp gztools.cpp leanify/zip.cpp leanify/leanify.cpp mozjpeg/.libs/libjpeg.a libpng/libpng.a zlib/libz.a -o ../ect
leanify/zip.cpp: In member function ‘uint32_t Zip::RecompressFile(unsigned char*, uint32_t, uint32_t, std::__cxx11::string, const ECTOptions&)’:
leanify/zip.cpp:214:54: warning: ignoring return value of ‘size_t fread(void*, size_t, size_t, FILE*)’, declared with attribute warn_unused_result [-Wunused-result]
     fread(data - size_leanified, 1, new_size, stream);
                                                      ^

Probably a check should be added to make sure that t0 is not NULL and tha tmp becomes no longer than MAXPATHLEN:

char* t0 = getcwd(0, MAXPATHLEN - 7 - extension..length());

From getcwd manual:

       The getcwd() function copies an absolute pathname of the current working directory to  the
       array pointed to by buf, which is of length size.

       If  the  length  of  the absolute pathname of the current working directory, including the
       terminating null byte, exceeds size bytes, NULL is returned, and errno is set  to  ERANGE;
       an application should check for this error, and allocate a larger buffer if necessary.

       As an extension to the POSIX.1-2001 standard, glibc's getcwd() allocates the buffer dynam‐
       ically using malloc(3) if buf is NULL.  In this case, the allocated buffer has the  length
       size  unless  size  is zero, when buf is allocated as big as necessary.  The caller should
       free(3) the returned buffer.
fhanau commented 7 years ago

9d8ff57 should fix the last warning. Feel free to reopen this if you still have problems.