Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@barracuda156
Copy link

  1. Apparently endif is in the wrong place now, as follows both from comment to the existing code and a fact that it fails to compile on 10.6. Fix that, so that the package can actually build. (I wonder how no one noticed this so far.)

  2. Simplify the macro for macOS version: there is no need to check if MAC_OS_X_VERSION_10_7 is defined, MAC_OS_X_VERSION_MIN_REQUIRED on its own works.

@barracuda156
Copy link
Author

What happens with current pki.h is this:

--->  Building R-PKI
xinstall: mkdir /opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_R_R-PKI/R-PKI/work/build
Executing:  cd "/opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_R_R-PKI/R-PKI/work/PKI" && /opt/local/bin/R CMD INSTALL . --library=/opt/local/var/macports/build/_opt_PPCSnowLeopardPorts_R_R-PKI/R-PKI/work/build --install-tests 
* installing *source* package ‘PKI’ ...
** package ‘PKI’ successfully unpacked and MD5 sums checked
** using staged installation
checking for pkg-config... yes
checking whether it knows about openssl... yes
configure: CFLAGS: 
configure: LIBS: -L/opt/local/libexec/openssl3/lib -lssl -lcrypto
checking for gcc... /opt/local/bin/gcc-mp-13
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether /opt/local/bin/gcc-mp-13 accepts -g... yes
checking for /opt/local/bin/gcc-mp-13 option to enable C11 features... none needed
checking for stdio.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... yes
checking for sys/stat.h... yes
checking for sys/types.h... yes
checking for unistd.h... yes
checking for openssl/ssl.h... yes
checking for openssl/rsa.h... yes
checking for library containing RSA_generate_key_ex... none required
checking for openssl/ssl.h... (cached) yes
checking for library containing SSL_CTX_load_verify_locations... none required
configure: creating ./config.status
config.status: creating src/Makevars
config.status: creating src/config.h
** libs
using C compiler: ‘gcc-mp-13 (MacPorts gcc13 13.2.0_4+stdlib_flag) 13.2.0’
Warning in system2("xcrun", "--show-sdk-path", TRUE, TRUE) :
  running command ''xcrun' --show-sdk-path 2>&1' had status 64
using SDK: ‘NA’‘NA’‘NA’‘NA’‘NA’‘NA’
/opt/local/bin/gcc-mp-13 -I"/opt/local/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isystem/opt/local/include/LegacySupport -I/opt/local/include    -isystem/opt/local/include/LegacySupport -I/opt/local/include    -fPIC  -pipe -Os -arch ppc  -c asn1.c -o asn1.o
/opt/local/bin/gcc-mp-13 -I"/opt/local/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isystem/opt/local/include/LegacySupport -I/opt/local/include    -isystem/opt/local/include/LegacySupport -I/opt/local/include    -fPIC  -pipe -Os -arch ppc  -c init.c -o init.o
/opt/local/bin/gcc-mp-13 -I"/opt/local/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isystem/opt/local/include/LegacySupport -I/opt/local/include    -isystem/opt/local/include/LegacySupport -I/opt/local/include    -fPIC  -pipe -Os -arch ppc  -c pem.c -o pem.o
/opt/local/bin/gcc-mp-13 -I"/opt/local/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isystem/opt/local/include/LegacySupport -I/opt/local/include    -isystem/opt/local/include/LegacySupport -I/opt/local/include    -fPIC  -pipe -Os -arch ppc  -c pgp.c -o pgp.o
/opt/local/bin/gcc-mp-13 -I"/opt/local/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -isystem/opt/local/include/LegacySupport -I/opt/local/include    -isystem/opt/local/include/LegacySupport -I/opt/local/include    -fPIC  -pipe -Os -arch ppc  -c pki-x509.c -o pki-x509.o
In file included from pki-x509.c:1:
pki-x509.c: In function 'PKI_digest':
pki.h:55:28: error: expected ';' before '{' token
   55 | #define SHA1(D,L,H) do (1) { if ((L) >= 4294967296L) SHA1(D,L,H) else CC_SHA1(D, (uint32_t)(L), H); break }
      |                            ^
pki-x509.c:480:9: note: in expansion of macro 'SHA1'
  480 |         SHA1(what, what_len, hash);
      |         ^~~~
pki-x509.c:481:9: error: expected 'while' before 'len'
  481 |         len = SHA_DIGEST_LENGTH;
      |         ^~~
pki.h:57:23: error: expected ';' before '{' token
   57 | #define SHA256 do (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
      |                       ^
pki-x509.c:484:9: note: in expansion of macro 'SHA256'
  484 |         SHA256(what, what_len, hash);
      |         ^~~~~~
pki-x509.c:485:9: error: expected 'while' before 'len'
  485 |         len = SHA256_DIGEST_LENGTH;
      |         ^~~
pki.h:59:20: error: expected ';' before '{' token
   59 | #define MD5 do (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
      |                    ^
pki-x509.c:488:9: note: in expansion of macro 'MD5'
  488 |         MD5(what, what_len, hash);
      |         ^~~
pki-x509.c:489:9: error: expected 'while' before 'len'
  489 |         len = MD5_DIGEST_LENGTH;
      |         ^~~
make: *** [pki-x509.o] Error 1

Once macros fixed, the package installs successfully.

@barracuda156
Copy link
Author

@s-u Hmm, it is tricker than I thought.

This code does not build at all, as long as it get triggered. It was obscured because of misplaced macro, so on 10.7+ it was never executed. On 10.6 it fails to compile (see above).

Now, once macro is moved to its intended (I believe) place, the code is not used on 10.6, but is now used on 10.7+. And then it also fails, with the same errors:

/opt/local/bin/clang-mp-15 -I"/opt/local/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/opt/local/include -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk    -I/opt/local/include -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk    -fPIC  -pipe -Os -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX14.sdk -arch arm64  -c pki-x509.c -o pki-x509.o
pki-x509.c:480:2: error: expected ';' after expression
        SHA1(what, what_len, hash);
        ^
./pki.h:54:28: note: expanded from macro 'SHA1'
#define SHA1(D,L,H) do (1) { if ((L) >= 4294967296L) SHA1(D,L,H) else CC_SHA1(D, (uint32_t)(L), H); break }
                           ^
pki-x509.c:480:2: error: expected 'while' in do/while loop
./pki.h:54:28: note: expanded from macro 'SHA1'
#define SHA1(D,L,H) do (1) { if ((L) >= 4294967296L) SHA1(D,L,H) else CC_SHA1(D, (uint32_t)(L), H); break }
                           ^
pki-x509.c:480:2: note: to match this 'do'
./pki.h:54:21: note: expanded from macro 'SHA1'
#define SHA1(D,L,H) do (1) { if ((L) >= 4294967296L) SHA1(D,L,H) else CC_SHA1(D, (uint32_t)(L), H); break }
                    ^
pki-x509.c:484:2: error: expected ';' after expression
        SHA256(what, what_len, hash);
        ^
./pki.h:56:23: note: expanded from macro 'SHA256'
#define SHA256 do (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                      ^
pki-x509.c:484:2: error: expected 'while' in do/while loop
./pki.h:56:23: note: expanded from macro 'SHA256'
#define SHA256 do (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                      ^
pki-x509.c:484:2: note: to match this 'do'
./pki.h:56:16: note: expanded from macro 'SHA256'
#define SHA256 do (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
               ^
pki-x509.c:488:2: error: expected ';' after expression
        MD5(what, what_len, hash);
        ^
./pki.h:58:20: note: expanded from macro 'MD5'
#define MD5 do (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                   ^
pki-x509.c:488:2: error: expected 'while' in do/while loop
./pki.h:58:20: note: expanded from macro 'MD5'
#define MD5 do (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                   ^
pki-x509.c:488:2: note: to match this 'do'
./pki.h:58:13: note: expanded from macro 'MD5'
#define MD5 do (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
            ^
6 errors generated.
make: *** [pki-x509.o] Error 1
ERROR: compilation failed for package ‘PKI’

This is on 14.4.1 with Clang.

How was it supposed to work?

@barracuda156 barracuda156 marked this pull request as draft April 5, 2024 11:25
@barracuda156
Copy link
Author

@s-u So the code was added in 238bc4f#diff-3ab4f2c730895f5d5def202e3a45ffaacc729ad717a488fe030ee1d4094473d1 and had already misplaced endif.

How was it tested?

Since officially R does not support < 10.7 or building for 32-bit on macOS, this code will never be compiled:

PKI/src/pki.h

Lines 45 to 59 in 927050d

#ifndef __LP64__
#undef SHA1
#define SHA1(D,L,H) CC_SHA1(D, (uint32_t)(L), H)
#undef SHA256
#define SHA256(D,L,H) CC_SHA256(D, (uint32_t)(L), H)
#undef MD5
#define MD5(D,L,H) CC_MD5(D, (uint32_t)(L), H)
#endif
#else /* !LP64 */
#undef SHA1
#define SHA1(D,L,H) do (1) { if ((L) >= 4294967296L) SHA1(D,L,H) else CC_SHA1(D, (uint32_t)(L), H); break }
#undef SHA256
#define SHA256 do (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
#undef MD5
#define MD5 do (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }

(Or am I missing something obvious here and totally wrong in my conclusions?)

@s-u
Copy link
Owner

s-u commented Apr 7, 2024

As noted, this was never tested due to lack of a test target. Since you are creating the PR - and thus have a way to test - please make sure it is a valid fix one way or another. The way I see it it should simply say while instead of do so that's what I'd suggest to start with.

@barracuda156
Copy link
Author

barracuda156 commented Apr 13, 2024

@s-u Could you please clarify, where is that code supposed to run? Just logically, leaving aside an issue that it fails to compile.

It follows from the comment to this line that the following chunk is aimed at 64-bit systems. However, in fact there is endif before it, so instead we get something completely different.

It is not just a matter of not having an archaic macOS to test: either macros are misplaced (which was my understanding) or comments to the code are misleading.
It does look that comments are correct though, and macros are misplaced:

PKI/src/pki.h

Lines 45 to 60 in 927050d

#ifndef __LP64__
#undef SHA1
#define SHA1(D,L,H) CC_SHA1(D, (uint32_t)(L), H)
#undef SHA256
#define SHA256(D,L,H) CC_SHA256(D, (uint32_t)(L), H)
#undef MD5
#define MD5(D,L,H) CC_MD5(D, (uint32_t)(L), H)
#endif
#else /* !LP64 */
#undef SHA1
#define SHA1(D,L,H) do (1) { if ((L) >= 4294967296L) SHA1(D,L,H) else CC_SHA1(D, (uint32_t)(L), H); break }
#undef SHA256
#define SHA256 do (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
#undef MD5
#define MD5 do (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
#endif /* LP64 */

I need you help to understand the case (this is not my code after all). I am not asking to test anything, I just need to figure out the logic, because currently it seems off.

There are three checks there:

  1. #if __APPLE__ (uncontroversial).
  2. #if defined MAC_OS_X_VERSION_10_7 && MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
  3. #ifndef __LP64__

The problem is with 2 and 3.

P. S. I tried replacing do with while, and on Sonoma it still fails (this is under an assumption that endif is misplaced currently):

pki-x509.c:480:2: error: expected ';' after expression
        SHA1(what, what_len, hash);
        ^
./pki.h:54:69: note: expanded from macro 'SHA1'
#define SHA1(D,L,H) while (1) { if ((L) >= 4294967296L) SHA1(D,L,H) else CC_SHA1(D, (uint32_t)(L), H); break }
                                                                    ^
pki-x509.c:480:2: error: expected ';' after break statement
./pki.h:54:110: note: expanded from macro 'SHA1'
#define SHA1(D,L,H) while (1) { if ((L) >= 4294967296L) SHA1(D,L,H) else CC_SHA1(D, (uint32_t)(L), H); break }
                                                                                                             ^
pki-x509.c:484:2: error: use of undeclared identifier 'L'
        SHA256(what, what_len, hash);
        ^
./pki.h:56:33: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                ^
pki-x509.c:484:2: error: expected ';' after expression
./pki.h:56:66: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                                                 ^
pki-x509.c:484:2: error: use of undeclared identifier 'D'
./pki.h:56:59: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                                          ^
pki-x509.c:484:2: error: use of undeclared identifier 'L'
./pki.h:56:61: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                                            ^
pki-x509.c:484:2: error: use of undeclared identifier 'H'
./pki.h:56:63: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                                              ^
pki-x509.c:484:2: error: use of undeclared identifier 'L'
./pki.h:56:95: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                                                                              ^
pki-x509.c:484:2: error: use of undeclared identifier 'D'
./pki.h:56:81: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                                                                ^
pki-x509.c:484:2: error: use of undeclared identifier 'H'
./pki.h:56:99: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                                                                                  ^
pki-x509.c:484:2: error: expected ';' after break statement
./pki.h:56:109: note: expanded from macro 'SHA256'
#define SHA256 while (1) { if ((L) >= 4294967296L) SHA256(D,L,H) else CC_SHA256(D, (uint32_t)(L), H); break }
                                                                                                            ^
pki-x509.c:488:2: error: use of undeclared identifier 'L'
        MD5(what, what_len, hash);
        ^
./pki.h:58:30: note: expanded from macro 'MD5'
#define MD5 while (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                             ^
pki-x509.c:488:2: error: expected ';' after expression
./pki.h:58:60: note: expanded from macro 'MD5'
#define MD5 while (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                                                           ^
pki-x509.c:488:2: error: use of undeclared identifier 'D'
./pki.h:58:53: note: expanded from macro 'MD5'
#define MD5 while (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                                                    ^
pki-x509.c:488:2: error: use of undeclared identifier 'L'
./pki.h:58:55: note: expanded from macro 'MD5'
#define MD5 while (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                                                      ^
pki-x509.c:488:2: error: use of undeclared identifier 'H'
./pki.h:58:57: note: expanded from macro 'MD5'
#define MD5 while (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                                                        ^
pki-x509.c:488:2: error: use of undeclared identifier 'L'
./pki.h:58:86: note: expanded from macro 'MD5'
#define MD5 while (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                                                                                     ^
pki-x509.c:488:2: error: use of undeclared identifier 'D'
./pki.h:58:72: note: expanded from macro 'MD5'
#define MD5 while (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                                                                       ^
pki-x509.c:488:2: error: use of undeclared identifier 'H'
./pki.h:58:90: note: expanded from macro 'MD5'
#define MD5 while (1) { if ((L) >= 4294967296L) MD5(D,L,H) else CC_MD5(D, (uint32_t)(L), H); break }
                                                                                         ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [pki-x509.o] Error 1

@s-u
Copy link
Owner

s-u commented Apr 17, 2024

As far as I can tell the purpose of that code (Apple-specific) was simply to not use the native implementation for large vectors, because the API only supports 32-bit length so it was supposed to dynamically switch based on the requested length. The #else branch is obviously borked as it is missing the macro parameters in the latter two macros - nesting made it to never trigger.

s-u added a commit that referenced this pull request May 20, 2024
@s-u
Copy link
Owner

s-u commented May 20, 2024

The PR was off, but good discussion, should be now fixed.

@s-u s-u closed this May 20, 2024
@barracuda156
Copy link
Author

@s-u Thank you very much! I will test the update.

P. S. Sorry, did not get back to this, since it was unclear how to fix it, and I am still stuck with rebuilding 4000+ R ports after the R upgrade.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants