|
13 | 13 | #else
|
14 | 14 | # include <unistd.h>
|
15 | 15 | #endif
|
16 |
| -#include <iostream> |
17 |
| -#include <fstream> |
18 | 16 | #include <cctype>
|
| 17 | +#include <cstdio> |
19 | 18 | #include <vector>
|
20 | 19 | #include <algorithm>
|
21 | 20 | #include <sys/stat.h>
|
@@ -462,21 +461,27 @@ namespace Sass {
|
462 | 461 | // just convert from unsigned char*
|
463 | 462 | char* contents = (char*) pBuffer;
|
464 | 463 | #else
|
| 464 | + // Read the file using `<cstdio>` instead of `<fstream>` for better portability. |
| 465 | + // The `<fstream>` header initializes `<locale>` and this buggy in GCC4/5 with static linking. |
| 466 | + // See: |
| 467 | + // https://www.spinics.net/lists/gcchelp/msg46851.html |
| 468 | + // https://github.com/sass/sassc-ruby/issues/128 |
465 | 469 | struct stat st;
|
466 | 470 | if (stat(path.c_str(), &st) == -1 || S_ISDIR(st.st_mode)) return 0;
|
467 |
| - std::ifstream file(path.c_str(), std::ios::in | std::ios::binary | std::ios::ate); |
468 |
| - char* contents = 0; |
469 |
| - if (file.is_open()) { |
470 |
| - size_t size = file.tellg(); |
471 |
| - // allocate an extra byte for the null char |
472 |
| - // and another one for edge-cases in lexer |
473 |
| - contents = (char*) malloc((size+2)*sizeof(char)); |
474 |
| - file.seekg(0, std::ios::beg); |
475 |
| - file.read(contents, size); |
476 |
| - contents[size+0] = '\0'; |
477 |
| - contents[size+1] = '\0'; |
478 |
| - file.close(); |
| 471 | + FILE* fd = std::fopen(path.c_str(), "rb"); |
| 472 | + if (fd == nullptr) return nullptr; |
| 473 | + const std::size_t size = st.st_size; |
| 474 | + char* contents = static_cast<char*>(malloc(st.st_size + 2 * sizeof(char))); |
| 475 | + if (std::fread(static_cast<void*>(contents), 1, size, fd) != size) { |
| 476 | + free(contents); |
| 477 | + return nullptr; |
479 | 478 | }
|
| 479 | + if (std::fclose(fd) != 0) { |
| 480 | + free(contents); |
| 481 | + return nullptr; |
| 482 | + } |
| 483 | + contents[size] = '\0'; |
| 484 | + contents[size + 1] = '\0'; |
480 | 485 | #endif
|
481 | 486 | std::string extension;
|
482 | 487 | if (path.length() > 5) {
|
|
0 commit comments