Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive - cgo - block starting/ending with whitespaces #162

Open
RickvanLoo opened this issue Jan 13, 2025 · 2 comments
Open

False positive - cgo - block starting/ending with whitespaces #162

RickvanLoo opened this issue Jan 13, 2025 · 2 comments

Comments

@RickvanLoo
Copy link

For the newest wsl (v4.5.0), given this code snippet:

// SetEventAlarm updates the EventAlarm settings.
func (e *Ecopoint) SetEventAlarm(alarm EventAlarm) error {
	ret := C.ecopointSetEventAlarm(
		e.instance,
		C.uint8_t(alarm.Count), C.uint8_t(alarm.BaseTime), C.uint8_t(alarm.LockTime), C.uint16_t(alarm.Threshold),
	)
	if ret != 0 {
		return getLastError()
	}

	return nil
}

I'm getting the following false positives:

ecopoint_clib.go:117:27: block should not start with a whitespace
ecopoint_clib.go:120:68: block should not end with a whitespace (or comment)

Pointing to the start and end of the ret block.

I'm still trying to isolate the issue, but I've not been able to produce it yet outside of our project.

@bombsimon
Copy link
Owner

bombsimon commented Jan 13, 2025

Thanks for this! CGO support was just added but not by me so I'm not 100% sure why this is but let's figure it out!

  • Are you running with -include-generated or not? This shouldn't matter, but want to double check.
  • What's your output of this function if you run go tool cgo -godefs your_file.go?
  • I failed to reproduce a fully running example for this, see below. If you have the time a fully working example that I can run would be much appreciated!

I tried to reproduce this code as a runnable/compiling example.

Show code
package main

// #include <stdio.h>
// #include <stdint.h>
// uint8_t hello(uint8_t a, uint8_t b, uint8_t c) {
//    printf("Hello from C: %d, %d, %d\n", a, b, c);
//
//    return 0;
//}
import "C"
import "errors"

func main() {
	PrintCHello()
}

func PrintCHello() error {
	ret := C.hello(
		C.uint8_t(1),
		C.uint8_t(2),
		C.uint8_t(3),
	)
	if ret != 0 {
		return errors.New("error")
	}

	return nil
}

However that doesn't produce your result:

› wsl ./

And the generated code does very few changes not causing any issues for the linter:

› go tool cgo -godefs main.go
Show code
// Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs main.go

package main

import "errors"

func main() {
        PrintCHello()
}

func PrintCHello() error {
        ret := _Cfunc_hello(
                uint8(1),
                uint8(2),
                uint8(3),
        )
        if ret != 0 {
                return errors.New("error")
        }

        return nil
}

@RickvanLoo
Copy link
Author

I've tried something similar and indeed, in-line C-code with a similar function does not produce the same issues. I've tried both -include-generated and not, and I do not see different results.

Running go tool cgo -godefs ecopoint_clib.go produces the following:

func (e *Ecopoint) SetEventAlarm(alarm EventAlarm) error {
        ret := _Cfunc_ecopointSetEventAlarm(
                e.instance,
                uint8(alarm.Count), uint8(alarm.BaseTime), uint8(alarm.LockTime), uint16(alarm.Threshold),
        )
        if ret != 0 {
                return getLastError()
        }

        return nil
}

Also nothing shocking...
Maybe potential relevant information here is that e.instance is of type unsafe.Pointer, and the C library is included in the package in the following manner:

/*
#cgo pkg-config: ecopoint

#include <stdlib.h>
#include <ecopointlib_24-04-2023.h>

*/

The ecopointlib we are using is proprietary, so I am unfortunately not allow to share it and neither do I know the implementation of the called function. I will try to find some time in the coming days to reproduce it using open source libs.

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

No branches or pull requests

2 participants