php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79075 FFI header parser chokes on comments
Submitted: 2020-01-07 21:45 UTC Modified: 2024-12-07 19:02 UTC
From: larry at garfieldtech dot com Assigned:
Status: Closed Package: FFI (PECL)
PHP Version: 7.4.1 OS: Ubuntu 19.10
Private report: No CVE-ID: None
 [2020-01-07 21:45 UTC] larry at garfieldtech dot com
Description:
------------
(I couldn't find an FFI section in the packages affected section; feel free to relocate this ticket to the right location.)

When using FFI, If the .h file contains a comment that comes before a #define then the FFI::load() will fail with a Warning: cannot resolve C function.

This does not seem to happen if the same .h file is used in FFI::cdef().

That is, the following header file works fine:

```
#define FFI_SCOPE "POINTS"
#define FFI_LIB "./points.so"

struct point {
    int     x;
    int     y;
};

// Foo

double distance(struct point first, struct point second);
```

But this version fails in FFI::load(), and works in FFI::cdef():

```
#define FFI_SCOPE "POINTS"
// Foo
#define FFI_LIB "./points.so"

struct point {
    int     x;
    int     y;
};

double distance(struct point first, struct point second);
```

I have verified the same behavior regardless of whether // or /* */ comments are used.

Test script:
---------------
See: https://github.com/Crell/ffi-test/tree/476610f469aff116f41131948e58b0e27f9b9a57

That commit has a directory "points" with sample code.  (It's fixed in more recent comments.)  To demonstrate, change into that directory and run "make inline".  That will work.  Then run "make preload", which uses the preload-with-load() approach.  It will fail.  Then modify the points.h file to remove the comment between the #define statements.  It will now work.

(At least that's what I experienced.)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2024-12-07 18:53 UTC] nielsdos@php.net
-Status: Open +Status: Verified -Package: Unknown/Other Function +Package: FFI
 [2024-12-07 18:53 UTC] nielsdos@php.net
I guess this was forgotten about because it probably predates the FFI bug category, I can confirm the behaviour and I moved it.
 [2024-12-07 19:02 UTC] nielsdos@php.net
I understand the issue, finding those FFI_SCOPE and FFI_LIB defines in a separate pre-processing step in zend_ffi_parse_directives(). So that would need comment support added...
 [2024-12-09 21:29 UTC] git@php.net
Automatic comment on behalf of nielsdos
Revision: https://github.com/php/php-src/commit/612a34cbec79e7549c725b682fe467fbad3b729a
Log: Fix bug #79075: FFI header parser chokes on comments
 [2024-12-09 21:29 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jan 18 04:01:28 2025 UTC