Segmentation fault in Enable features

Hi OpenFHE team!
I met a bug about CryptoContext setting(In bfv,bgv,ckks schme). I accidentally setting the CryptoContext to nullptr. and I turn on the features(LevelSHE etc). it pop up a segmentation fault. So I think there are should be checking before you let the features enable to use.

Here is the MWE:

int main(int argc, char* argv[]) {
    uint32_t multDepth = 1;
    uint32_t scaleModSize = 50; 
    CCParams<CryptoContextBGVRNS> parameters;
    
    parameters.SetPlaintextModulus(256);
    parameters.SetMultiplicativeDepth(multDepth);
    parameters.SetScalingModSize(scaleModSize);
    parameters.SetScalingTechnique(FIXEDMANUAL); 
    parameters.SetFirstModSize(60);
    CryptoContext<DCRTPoly> cc = nullptr;
    

    std::string version = GetOPENFHEVersion();
    std::cout << "OpenFHE version: " << version << std::endl;

    std::cout << parameters << std::endl;

    cc->Enable(PKE);
    cc->Enable(LEVELEDSHE);
    cc->Enable(KEYSWITCH);
    cc->Enable(ADVANCEDSHE);
}

Output:

OpenFHE version: 1.2.1
scheme: CKKSRNS; ptModulus: 256; digitSize: 0; standardDeviation: 3.19; secretKeyDist: UNIFORM_TERNARY; maxRelinSkDeg: 2; ksTech: HYBRID; scalTech: FIXEDMANUAL; batchSize: 0; firstModSize: 60; numLargeDigits: 0; multiplicativeDepth:1; scalingModSize: 50; securityLevel: HEStd_128_classic; ringDim: 0; evalAddCount: 0; keySwitchCount: 0; encryptionTechnique: STANDARD; multiplicationTechnique: HPS; multiHopModSize: 0; PREMode: INDCPA; multipartyMode: FIXED_NOISE_MULTIPARTY; executionMode: EXEC_EVALUATION; decryptionNoiseMode: FIXED_NOISE_DECRYPT; noiseEstimate: 0; desiredPrecision: 25; statisticalSecurity: 30; numAdversarialQueries: 1; ThresholdNumOfParties: 1
Segmentation fault

Asan(AddressSanitizer is a powerful memory error detection utility) Output:

AddressSanitizer:DEADLYSIGNAL
=================================================================
==4040634==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000018 (pc 0x000000405617 bp 0x0fffda49b5a8 sp 0x7ffed24dad40 T0)
==4040634==The signal is caused by a READ memory access.
==4040634==Hint: address points to the zero page.
    #0 0x405616  (/data/homomorphic/test-openfhe/new-build/bug+0x405616)
    #1 0x7fa39d232082  (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    #2 0x4059ed  (/data/homomorphic/test-openfhe/new-build/bug+0x4059ed)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/data/homomorphic/test-openfhe/new-build/bug+0x405616) 

Is this a bug?

I would really appreciate any insights or explanations regarding this behavior.
Thank you for your help!

Best regards,
wowblk

CryptoContext<DCRTPoly> cc = nullptr;
You initialized the crypto-context as a null pointer, and then you tried to call member functions of the crypto-context. Initialize the crypto-context properly as shown in the examples, most likely you will get rid of this particular error at least.

@wowblk8 if you set ANY pointer to nullptr intentionally and try to use it then you will definitely get a segfault.

yes definitely. But I think there are should be a type check before you enable to openfhe feature, otherwise maybe in a big application based on openfhe in someways will trigger this segmentation fault.

Of course!This is a basic C++ language problem. But I think such a segmentation fault may be triggered in some openfhe-based programs. So it would be better to add a typecheck code i think.

Are there any developers here?:joy:

Hi @wowblk8,

This discussion is distracting both for OpenFHE developers and users. The question has already been answered. Many examples are provided on how to use the library, and none of them set the crypto context to nullptr. I suggest looking at C++ 101 resources (for beginners) to learn how to work with shared points. Please do not post similar questions/comments to the Discourse forum as the community here is focused on building applications based on FHE rather than discussing basic C++ questions. I appreciate your understanding.