@@ -323,9 +323,9 @@ func probeWindowing(ef *elf.File, info *LinuxInfo) {
323323 if s := ef .Section (".rodata" ); s != nil && s .Size <= maxRodataScan {
324324 data , _ = s .Data ()
325325 }
326- has := func (needle string ) bool { return bytes . Contains (data , [] byte ( needle ) ) }
326+ has := func (needle string ) bool { return hasCString (data , needle , false ) }
327327 for _ , d := range displayLibraries {
328- if has ( d .prefix ) {
328+ if hasCString ( data , d .prefix , true ) {
329329 found [d .tag ] = true
330330 }
331331 }
@@ -335,10 +335,11 @@ func probeWindowing(ef *elf.File, info *LinuxInfo) {
335335 info .SDL , info .SDLBundled , info .SDLDynamicAPI = "2" , true , true
336336 case has ("SDL3_DYNAMIC_API" ):
337337 info .SDL , info .SDLBundled , info .SDLDynamicAPI = "3" , true , true
338- case has ("SDL_VIDEODRIVER" ):
339- info .SDL , info .SDLBundled = "2" , true
338+ // SDL3 still reads the SDL2 name, so its own name goes first
340339 case has ("SDL_VIDEO_DRIVER" ):
341340 info .SDL , info .SDLBundled = "3" , true
341+ case has ("SDL_VIDEODRIVER" ):
342+ info .SDL , info .SDLBundled = "2" , true
342343 }
343344 }
344345
@@ -348,6 +349,28 @@ func probeWindowing(ef *elf.File, info *LinuxInfo) {
348349 sort .Strings (info .Display )
349350}
350351
352+ // hasCString reports whether needle is a whole NUL-terminated string in
353+ // data. With prefix, needle is a library file name that may continue past
354+ // the match and may end a path, as in "/libGL.so.1". Go and other languages
355+ // that pack literals without terminators never match, so a tool that embeds
356+ // these needles (butler embeds dash) does not detect itself.
357+ func hasCString (data []byte , needle string , prefix bool ) bool {
358+ n := []byte (needle )
359+ for off := 0 ; ; {
360+ i := bytes .Index (data [off :], n )
361+ if i < 0 {
362+ return false
363+ }
364+ i += off
365+ end := i + len (n )
366+ start := i == 0 || data [i - 1 ] == 0 || (prefix && data [i - 1 ] == '/' )
367+ if start && (prefix || (end < len (data ) && data [end ] == 0 )) {
368+ return true
369+ }
370+ off = i + 1
371+ }
372+ }
373+
351374// ProbeELF returns the full Linux record for one executable: what the
352375// magic pass reads from the header plus what DeepProbe adds. It is for
353376// tools that look at a single file rather than an install folder.
0 commit comments