[-] ghodawalaaman@programming.dev 1 points 1 week ago

could you share some resources please?

[-] ghodawalaaman@programming.dev 2 points 1 week ago

welcome!

I am currently working on https://spaidyslabs.com/ it's a project of my friend. feel free to join or collaborate! :D

( oh and if you find any vulnerabilities which there are a lot please report it don't exploit it :) )

[-] ghodawalaaman@programming.dev 1 points 1 week ago

no 🤧

I mean yeah I used AI but it's not entirely vibe coded.

[-] ghodawalaaman@programming.dev 3 points 1 week ago

well if a person decide to use this attack small OSS projects server then we are failed as humanity. I shared this article to fight against big tech surveillance if people use it to damage FOSS project I highly discourage that behavior.

[-] ghodawalaaman@programming.dev 1 points 1 week ago

that's exactly what happened with recent project I worked with it's https://spaidyslabs.com/ if you are interested. we just shove whatever worked at the time of developing it and now it just a mess!

no policies protecting the supabase, all the supabase calls are coming from client instead of the backend which makes it so difficult to make it secure. 😭

at this point I think we need a entire rewrite of the database and the nextjs code which takes time and effort 😭😭😭

[-] ghodawalaaman@programming.dev 1 points 2 weeks ago

What policies are preventing users from inserting data? okay, I just got confused there for a bit actually what's happening is that I have created a policy on SELECT to prevent other users from accessing data of other users and it looks something like auth.uid() = user_id. iirc the policy to prevent INSERT looks something like this: auth.role() = 'authenticated'::text() so yeah only authenticated users can insert data but that doesn't guaranty that client/user/browser will insert correct data.

If you are asking this question then you very likely should not be doing what you’re doing. yes, I know that's why I am asking for suggestions, I don't have much experience in either supabase or Nextjs but I am learning :)

There are ways to do it safely, but it’s for very very specific circumstances, with very very specific security setups. okay, so what do you suggest I should do. I can't just shove more policies into the supabase to make it secure I think so the only way to make it secure is to have the server ( vercel ) do all the supabase calls and don't share the supabase url so that the client can't just query supabase. but again the reason I am not doing this is that it will require a very big refactor throughout the codebase. ( which I am terrified of T.T )

[-] ghodawalaaman@programming.dev 2 points 1 month ago

Oh so that's why I was seeing a lot of post from that account, I thought they were generous😨

[-] ghodawalaaman@programming.dev 1 points 3 months ago

Nice! Thank you!

[-] ghodawalaaman@programming.dev -3 points 3 months ago

If no then why nobody has made it already?

3

Hello o/

I was experimenting with writing a language implementation so bump into this new thing called "recursive descent parser" at first it seemed complex but as I programmed it everything magically worked. I will attach the code tell me if I am wrong somewhere.

namespace ContextFreeGrammarDemo
{
    static class Parser
    {
        static string code = "aaaabbbb";
        static int cursor = 0;

        public static void Parse()
        {
            if (cursor >= code.Length)
                return;
            char currentChar = code[cursor];

            if (currentChar == 'a')
            {
                Console.WriteLine("a");
                cursor++;
                Parse();
                if (cursor < code.Length && code[cursor] == 'b')
                {
                    Console.WriteLine("b");
                    cursor++;
                    Parse();
                }
                else
                {
                    Console.WriteLine("Oopsie");
                    Environment.Exit(1);
                }
            }
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            Parser.Parse();
        }
    }
}
3
Aman Ghodawala's Website (sites.google.com)

This is my personal site where I will upload c sharp content, I hope you will like it :)

5

Platform Invoke (P/Invoke)

P/Invoke is a technology that allows you to call c/c++ function or function from a dll library from your managed code. It's divided into two namespace System and System.Runtime.InteropServices. you only need these namespaces to call external c/c++ function.

why do we need to call c/c++ function inside c#

  • It's more efficient than writing your own in c#
  • decreases code repetition

Windows Example

Here is an example of how you can use P/Invoke to call function from user32.dll.

using System;
using System.Runtime.InteropServices;

public partial class Program
{
    // Import user32.dll (containing the function we need) and define
    // the method corresponding to the native function.
    [LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16, SetLastError = true)]
    private static partial int MessageBoxW(IntPtr hWnd, string lpText, string lpCaption, uint uType);

    public static void Main(string[] args)
    {
        // Invoke the function as a regular managed method.
        MessageBoxW(IntPtr.Zero, "Command-line message box", "Attention!", 0);
    }
}

what happening in the snippet:

  • we have imported System and System.Runtime.InteropServices which contains the necessary attributes to call a function in a dll file.
  • the library user32.dll will be loaded at runtime and it will complete the implementation of MessageBoxW function. ( for those of who don't know what the function do: it's just showing a prompt to the user). keep in mind that this example is windows specific, it will not work on Linux or MacOS.
  • you have probably noticed that we doing StringMarshalling. ( Marshalling is the process of transforming types when they need to cross between c# to native code (c/c++ or dll))
  • you must define the c# function with the same signature of the c counterpart.

MacOS Example

using System;
using System.Runtime.InteropServices;

namespace PInvokeSamples
{
    public static partial class Program
    {
        // Import the libSystem shared library and define the method
        // corresponding to the native function.
        [LibraryImport("libSystem.dylib")]
        private static partial int getpid();

        public static void Main(string[] args)
        {
            // Invoke the function and get the process ID.
            int pid = getpid();
            Console.WriteLine(pid);
        }
    }
}
  • In the example we are loading libSystem.dylib library and calling a getpid() function inside that library.

Linux example

using System;
using System.Runtime.InteropServices;

namespace PInvokeSamples
{
    public static partial class Program
    {
        // Import the libc shared library and define the method
        // corresponding to the native function.
        [LibraryImport("libc.so.6")]
        private static partial int getpid();

        public static void Main(string[] args)
        {
            // Invoke the function and get the process ID.
            int pid = getpid();
            Console.WriteLine(pid);
        }
    }
}

Invoking managed code from unmanaged code

c# allows us to pass a callback function to unmanaged code which will be called when a particular event gets triggered. The closes thing to a function pointer in managed code is a delegate. keep in mind that you have to create the delegate which has the same signature as expected in the dll function.

Here is an example:

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    public static partial class Program
    {
        // Define a delegate that corresponds to the unmanaged function.
        private delegate bool EnumWC(IntPtr hwnd, IntPtr lParam);

        // Import user32.dll (containing the function we need) and define
        // the method corresponding to the native function.
        [LibraryImport("user32.dll")]
        private static partial int EnumWindows(EnumWC lpEnumFunc, IntPtr lParam);

        // Define the implementation of the delegate; here, we simply output the window handle.
        private static bool OutputWindow(IntPtr hwnd, IntPtr lParam)
        {
            Console.WriteLine(hwnd.ToInt64());
            return true;
        }

        public static void Main(string[] args)
        {
            // Invoke the method; note the delegate as a first parameter.
            EnumWindows(OutputWindow, IntPtr.Zero);
        }
    }
}

I hope you learned something new today!

3

Hello o/

I have been trying to get my forth interpreter working but I can't even parse it properly without error. here is what I have done so for ( it's my 3rd attempt ). if you know any good resources to learn compiler design then please link it. Thank you in advance!

namespace test;

public enum TokenType
{
    Number,
    Operator,
    String,
    Identifier,
    FunctionStart,
    FunctionEnd,
}
public class Token
{
    public TokenType Type { get; set; }
    public string Value { get; set; }

    public override string ToString()
    {
        return $"{Type}: {Value}";
    }
}

public class Program
{
    public static void Err()
    {
        Console.WriteLine("error");
    }
    public static void Main(string[] args)
    {
        string code = "12 12  : abc 10 . ;          23 - + ";

        List<Token> tokens = new List<Token>();
        bool stringMode = false;
        int startStringIndex = 0;
        for (int cur = 0; cur < code.Length; cur++)
        {
            char ch = code[cur];
            if (stringMode)
            {
                if (ch == '"' && code[cur-1] != '\\')
                {
                    stringMode = false;
                    string strValue = code.Substring(startStringIndex, cur - startStringIndex);
                    tokens.Add(new Token { Type = TokenType.String, Value = strValue });
                }
            }
            switch (ch)
            {
                case '+':
                case '-':
                case '*':
                case '/':
                    {
                        tokens.Add(new Token { Type = TokenType.Operator, Value = ch.ToString() });
                        break;
                    }
                case '.':
                    {
                        if (cur + 1 < code.Length && code[cur+1] == '"') 
                        {
                            stringMode = true;
                            cur+=2; // Skip the opening quote
                            startStringIndex = cur;
                        } else
                        {
                            tokens.Add(new Token { Type = TokenType.Operator, Value = ch.ToString() });
                        }
                        break;
                    }
                case >= '0' and <= '9':
                    {
                        int start = cur;
                        while (cur + 1 < code.Length && code[cur + 1] >= '0' && code[cur + 1] <= '9')
                        {
                            cur++;
                        }
                        string numberValue = code.Substring(start, cur - start + 1);
                        tokens.Add(new Token { Type = TokenType.Number, Value = numberValue });
                        break;
                    }
                case (>= 'a' and <= 'z') or (>= 'A' and <= 'Z'):
                    {
                        int start = cur;
                        while (cur + 1 < code.Length && ((code[cur + 1] >= 'a' && code[cur + 1] <= 'z') || (code[cur + 1] >= 'A' && code[cur + 1] <= 'Z') || (code[cur + 1] >= '0' && code[cur + 1] <= '9') || code[cur + 1] == '_'))
                        {
                            cur++;
                        }
                        string identifierValue = code.Substring(start, cur - start + 1);
                        tokens.Add(new Token { Type = TokenType.Identifier, Value = identifierValue });
                        break;
                    }
                case ':':
                    {
                        tokens.Add(new Token { Type = TokenType.FunctionStart, Value = ch.ToString() });
                        break;
                    }
                case ';':
                    {
                        tokens.Add(new Token { Type = TokenType.FunctionEnd, Value = ch.ToString() });
                        break;
                    }
                case ' ':
                case '\n':
                    {
                        break;
                    }
                default:
                    {
                        Err();
                        Console.WriteLine("Something went wrong!");
                        break;
                    }
            }
        }

        foreach (var token in tokens)
        {
            Console.WriteLine(token);
        }
    }
}

10

Hello,

I have been trying to learn c# from dotnet tutorial and they gave this code which supposed to trigger garbage collector and in turn call the Destructor of the class. however when I run the same code, I get result of Constructor only and not the Destructor.

using System;
namespace DestructorExample
{
    class DestructorDemo
    {
        public DestructorDemo()
        {
            Console.WriteLine("Constructor Object Created");
        }
        ~DestructorDemo()
        {
            string type = GetType().Name;
            Console.WriteLine($"Object {type} is Destroyed");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DestructorDemo obj1 = new DestructorDemo();
            DestructorDemo obj2 = new DestructorDemo();

            //Making obj1 for Garbage Collection
            obj1 = null;
            GC.Collect();
            Console.ReadKey();
        }
    }
}

here is the code, if you know please tell me why it's not working

view more: ‹ prev next ›

ghodawalaaman

joined 3 months ago