Skip to content

Commit

Permalink
fix - Fixed error handler related segfaults
Browse files Browse the repository at this point in the history
We've fixed segmentation faults related to error strings.

---

When getting error strings, we must cast from the pointer to the string
to get the error string. Otherwise, we get segfaults.

---

Type: fix
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Aug 28, 2023
1 parent 53dd572 commit 2535294
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions BassBoom.Basolia/BasoliaException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@

using BassBoom.Native.Interop.Init;
using System;
using System.Runtime.InteropServices;

namespace BassBoom.Basolia
{
public class BasoliaException : Exception
{
public BasoliaException(mpg123_errors error) :
base($"General Basolia error\n" +
$"MPG123 returned the following error: [{error}]")
$"MPG123 returned the following error: [{error} - {Marshal.PtrToStringAnsi(NativeError.mpg123_plain_strerror((int)error))}]")
{ }

public BasoliaException(string message, mpg123_errors error) :
base($"{message}\n" +
$"MPG123 returned the following error: [{error}]")
$"MPG123 returned the following error: [{error} - {Marshal.PtrToStringAnsi(NativeError.mpg123_plain_strerror((int)error))}]")
{ }

public BasoliaException(string message, Exception innerException, mpg123_errors error) :
base($"{message}\n" +
$"MPG123 returned the following error: [{error}]", innerException)
$"MPG123 returned the following error: [{error} - {Marshal.PtrToStringAnsi(NativeError.mpg123_plain_strerror((int)error))}]", innerException)
{ }
}
}
7 changes: 4 additions & 3 deletions BassBoom.Basolia/BasoliaOutException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@

using BassBoom.Native.Interop.Output;
using System;
using System.Runtime.InteropServices;

namespace BassBoom.Basolia
{
public class BasoliaOutException : Exception
{
public BasoliaOutException(out123_error error) :
base($"General Basolia output system error\n" +
$"OUT123 returned the following error: [{error}]")
$"OUT123 returned the following error: [{error} - {Marshal.PtrToStringAnsi(NativeOutputLib.out123_plain_strerror((int)error))}]")
{ }

public BasoliaOutException(string message, out123_error error) :
base($"{message}\n" +
$"OUT123 returned the following error: [{error}]")
$"OUT123 returned the following error: [{error} - {Marshal.PtrToStringAnsi(NativeOutputLib.out123_plain_strerror((int)error))}]")
{ }

public BasoliaOutException(string message, Exception innerException, out123_error error) :
base($"{message}\n" +
$"OUT123 returned the following error: [{error}]", innerException)
$"OUT123 returned the following error: [{error} - {Marshal.PtrToStringAnsi(NativeOutputLib.out123_plain_strerror((int)error))}]", innerException)
{ }
}
}
5 changes: 2 additions & 3 deletions BassBoom.Native/Interop/Init/NativeError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,13 @@ public static unsafe class NativeError
/// MPG123_EXPORT const char* mpg123_plain_strerror(int errcode);
/// </summary>
[DllImport(LibraryTools.LibraryName, CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.LPStr)]
internal static extern string mpg123_plain_strerror(int errcode);
internal static extern IntPtr mpg123_plain_strerror(int errcode);

/// <summary>
/// MPG123_EXPORT const char* mpg123_strerror(mpg123_handle *mh);
/// </summary>
[DllImport(LibraryTools.LibraryName, CharSet = CharSet.Ansi)]
internal static extern string mpg123_strerror(mpg123_handle* mh);
internal static extern IntPtr mpg123_strerror(mpg123_handle* mh);

/// <summary>
/// MPG123_EXPORT int mpg123_errcode(mpg123_handle *mh);
Expand Down
4 changes: 2 additions & 2 deletions BassBoom.Native/Interop/Output/NativeOutputLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static unsafe class NativeOutputLib
/// MPG123_EXPORT const char* out123_strerror(out123_handle *ao);
/// </summary>
[DllImport(LibraryTools.LibraryNameOut, CharSet = CharSet.Ansi)]
internal static extern string out123_strerror(out123_handle* ao);
internal static extern IntPtr out123_strerror(out123_handle* ao);

/// <summary>
/// MPG123_EXPORT int out123_errcode(out123_handle *ao);
Expand All @@ -131,7 +131,7 @@ public static unsafe class NativeOutputLib
/// MPG123_EXPORT const char* out123_plain_strerror(int errcode);
/// </summary>
[DllImport(LibraryTools.LibraryNameOut, CharSet = CharSet.Ansi)]
internal static extern string out123_plain_strerror(int errcode);
internal static extern IntPtr out123_plain_strerror(int errcode);

/// <summary>
/// MPG123_EXPORT int out123_set_buffer(out123_handle *ao, size_t buffer_bytes);
Expand Down
Empty file modified tools/build.sh
100644 → 100755
Empty file.

0 comments on commit 2535294

Please sign in to comment.