Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #70412

Re: One agnostic random function for big project (4 GB of code)

From Paavo Helde <myfirstname@osa.pri.ee>
Newsgroups comp.lang.c++
Subject Re: One agnostic random function for big project (4 GB of code)
Date 2019-12-12 15:15 +0200
Organization A noiseless patient Spider
Message-ID <qstei1$9ki$1@dont-email.me> (permalink)
References <6803eb03-e599-47eb-9e1b-fc064e5df946@googlegroups.com> <qstbuv$pem$1@dont-email.me> <583fe6f3-5f6a-4d46-9baf-2da839d7b6d8@googlegroups.com>

Show all headers | View raw


On 12.12.2019 14:47, Frederick Gotham wrote:
> On Thursday, December 12, 2019 at 12:31:43 PM UTC, Paavo Helde wrote:
>
>>> extern "C" void *dlopen(char const *, int);
>>> extern "C" void *dlsym(void *,char const *);
>>>
>>
>> Namespace is missing. All C++ code should be in a namespace, especially
>> if used so widely.
>
>
> "libdl.so" is a dynamically-loaded shared library on Linux, and it's written in C. If you know how to link with a C library and put everything inside a namespace then please tell me how. I can't change the "libdl.so" library in this project.
>

Sorry, I meant that your Agnostic_Random() should be in a namespace.

C library interfaces are indeed in the global namespace, that's one of 
the reasons why all C++ code should be in a namespace.

>
>> Buffer length should be in size_t, not some random type.
>
>
> I chose uintmax_t specifically because I want the biggest native type. I could always use a "bignum" library if I wanted even bigger. (Although 16.8 million terabytes can be addressed with a 64-Bit unsigned int).

The caller cannot have a buffer which does not fit in std::size_t. Using 
anything else is just obfuscation.

>
>> The handle is leaked. There should be a matching call to dlclose(). This
>> could be achieved pretty easily by having a static C++ class object with
>> a destructor.
>
>
> Linux handles all that when the process dies.

That's why I wrote "should", not "must".

> 		
>> A race condition. There is a time slice when the flag is set, but the
>> function pointer is not yet assigned. If another thread calls this
>> function during this time, it will see a wrong (NULL) function pointer.
>>
>> Instead suggesting a simple:
>>
>> static RAND_bytes_t ptr_to_RAND_bytes = SomeInitFunction();
>>
>> This is guaranteed to do the correct thing since C++11.
>
>
> Thank you, I missed this race condition.

You are welcome!

> <snip>
>> No error return or exception throw. Extremely bad style, especially if
>> this has anything to do with crypto.
> <snip>
>> No error check! Extremely bad style, especially in anything related to
>> crypto!
>
>
> That's another day's work..... I've to chat to the team(s) about what should happen when we can't get random numbers from the TPM2 chip. I might even have to power off the unit.

Seems a good idea.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 03:53 -0800
  Re: One agnostic random function for big project (4 GB of code) Bart <bc@freeuk.com> - 2019-12-12 12:01 +0000
    Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 04:06 -0800
      Re: One agnostic random function for big project (4 GB of code) boltar@nowhere.co.uk - 2019-12-12 12:22 +0000
      Iceream is your friend (was Re: One agnostic random function for big project (4 GB of code)) Vir Campestris <vir.campestris@invalid.invalid> - 2019-12-13 21:51 +0000
  Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 14:31 +0200
    Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 04:47 -0800
      Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 15:15 +0200
        Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 05:27 -0800
          Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 16:25 +0200
            Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 06:33 -0800
            Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 16:40 +0200
              Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 07:26 -0800
                Re: One agnostic random function for big project (4 GB of code) Paavo Helde <myfirstname@osa.pri.ee> - 2019-12-12 17:37 +0200
                Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 07:48 -0800
                Re: One agnostic random function for big project (4 GB of code) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-12-12 08:27 -0800
    Re: One agnostic random function for big project (4 GB of code) Manfred <noname@add.invalid> - 2019-12-12 17:41 +0100

csiph-web