Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. segmentation error while using mpz_powm in a loop

segmentation error while using mpz_powm in a loop

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelp
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S shrims4u

    i am writing a milter which decrypts the email message. For decryption i am using GMP library... The code which gives the error is as follows: mpz_t bigbody,encbody,decbody,n,d,e; mpz_init_set_str (e, "5", 10); mpz_init_set_str (n, "81072118814004638617775947138239917744589505176471539666269773100315781097546894455560658571445286325125766632676152452856383447352821337178984474781813475276075048299705214136395672502245955793627681061360075723595075874516950614187016991430293887968216015097675955399134514678660046996064181098182893072761", 10); mpz_init_set_str (d, "32428847525601855447110378855295967097835802070588615866507909240126312439018757782224263428578114530050306653070460981142553378941128534871593789912725382636961637010791821646038659077539848411532121133465246631106852802485583932352673149866783600963924490576147444236696494852521901563664557493382382127469", 10); char *STR; STR=(char*)malloc(1000); mpz_t array[10]; //mpz_array_init (array, 10, 308); mpz_t array1[10]; //mpz_array_init (array1, 10, 100000); char substring[400]; int i=0,lower=0,upper=308; while(iconnection_num, substring ); lower=upper; upper=upper+308; mpz_init_set_str(array[i],substring,10); mpz_get_str (STR, 10, array[i]); strcat(STR,"\0"); syslog( LOG_INFO, "conn# %d - Encrypted-Biginteger = %s", cd->connection_num, STR ); mpz_init(array1[i]); mpz_powm (array1[i], array[i], d, n); mpz_get_str (STR, 10, array1[i]); syslog( LOG_INFO, "conn# %d - Decrypted-Biginteger = %s", cd->connection_num, STR ); size_t length; void *ret; char *decrypted; decrypted=(char*)malloc(1000); ret=mpz_export (decrypted, &length, 1,sizeof(data_buffer[0]), 0, 0, array1[i]); //ret=mpz_export (decrypted, &length, 1,sizeof(data_buffer[0]), 0, 0, array1[i]); strncpy(decrypted,ret,length); *(decrypted+length)='\0'; syslog( LOG_INFO, "conn# %d - size decrypted = %d Decrypted = %s", cd->connection_num,length, (char *)ret ); fflush(stdout); free(decrypted); fflush(stdin); mpz_clear(array[i]); mpz_clear(array1[i]); i++; } This code executes for 2 iterations and then give segmentation fault at mpz_powm( )

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    shrims4u wrote:

    ...then give segmentation fault at mpz_powm( )

    What are the values of array1[i], array[i], d, and n at that point?

    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    S 1 Reply Last reply
    0
    • D David Crow

      shrims4u wrote:

      ...then give segmentation fault at mpz_powm( )

      What are the values of array1[i], array[i], d, and n at that point?

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      S Offline
      S Offline
      shrims4u
      wrote on last edited by
      #3

      the value of array[i] is a bignumber.... Also d and n are initialised... The error is at mpz_powm.... you want me to write the values in numbers???? Actually it differs each time according to the message

      D 1 Reply Last reply
      0
      • S shrims4u

        the value of array[i] is a bignumber.... Also d and n are initialised... The error is at mpz_powm.... you want me to write the values in numbers???? Actually it differs each time according to the message

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        My, point, although subtle, was to ensure that the values you are passing to that function are valid. Some functions/libraries place the onus of validation solely on the caller (rather than handle the bad input and fail gracefully)

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        S J 3 Replies Last reply
        0
        • D David Crow

          My, point, although subtle, was to ensure that the values you are passing to that function are valid. Some functions/libraries place the onus of validation solely on the caller (rather than handle the bad input and fail gracefully)

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          S Offline
          S Offline
          shrims4u
          wrote on last edited by
          #5

          yah the values that are passed are proper... The gdb output is as follows.... [New Thread -1208882288 (LWP 9012)] [New Thread -1208879424 (LWP 9009)] [New Thread -1219372144 (LWP 9017)] GNU MP: Cannot allocate memory (size=3368600780) Program received signal SIGABRT, Aborted. [Switching to Thread -1219372144 (LWP 9017)] 0x00110402 in __kernel_vsyscall ()

          M 1 Reply Last reply
          0
          • D David Crow

            My, point, although subtle, was to ensure that the values you are passing to that function are valid. Some functions/libraries place the onus of validation solely on the caller (rather than handle the bad input and fail gracefully)

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            S Offline
            S Offline
            shrims4u
            wrote on last edited by
            #6

            Also i get this message from gdb..... Program received signal SIGSEGV, Segmentation fault. [Switching to Thread -1218634864 (LWP 9064)] 0x06d0199c in __gmpz_powm () from /usr/lib/sse2/libgmp.so.3

            1 Reply Last reply
            0
            • D David Crow

              My, point, although subtle, was to ensure that the values you are passing to that function are valid. Some functions/libraries place the onus of validation solely on the caller (rather than handle the bad input and fail gracefully)

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              J Offline
              J Offline
              jeron1
              wrote on last edited by
              #7

              You certainly did not deserve to be down voted for that response.

              S 1 Reply Last reply
              0
              • S shrims4u

                yah the values that are passed are proper... The gdb output is as follows.... [New Thread -1208882288 (LWP 9012)] [New Thread -1208879424 (LWP 9009)] [New Thread -1219372144 (LWP 9017)] GNU MP: Cannot allocate memory (size=3368600780) Program received signal SIGABRT, Aborted. [Switching to Thread -1219372144 (LWP 9017)] 0x00110402 in __kernel_vsyscall ()

                M Offline
                M Offline
                molesworth
                wrote on last edited by
                #8

                shrims4u wrote:

                GNU MP: Cannot allocate memory (size=3368600780)

                I would guess that this is related to the problem. It's tried to allocate 3 Gb of memory, and failed... As to why it's trying to allocate such a big chunk of memory, I couldn't say, but maybe stepping through until you get that error might help pin it down.

                There are three kinds of people in the world - those who can count and those who can't...

                S 2 Replies Last reply
                0
                • J jeron1

                  You certainly did not deserve to be down voted for that response.

                  S Offline
                  S Offline
                  shrims4u
                  wrote on last edited by
                  #9

                  i dint understand??? wat is down voting???

                  J 1 Reply Last reply
                  0
                  • M molesworth

                    shrims4u wrote:

                    GNU MP: Cannot allocate memory (size=3368600780)

                    I would guess that this is related to the problem. It's tried to allocate 3 Gb of memory, and failed... As to why it's trying to allocate such a big chunk of memory, I couldn't say, but maybe stepping through until you get that error might help pin it down.

                    There are three kinds of people in the world - those who can count and those who can't...

                    S Offline
                    S Offline
                    shrims4u
                    wrote on last edited by
                    #10

                    ill mail the whole program to uall Actually it is a milter which decrypts the incoming mail messages... /* sample_milter - example mail filter module ** ** Copyright © 2004 by Jef Poskanzer . ** All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions ** are met: ** 1. Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** 2. Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in the ** documentation and/or other materials provided with the distribution. ** ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ** SUCH DAMAGE. ** ** For commentary on this license please see http://www.acme.com/license.html */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "libmilter/mfapi.h" ////////////////////////////////////////////////////////////// #include "gmp.h" /* Forwards. */ static void usage( void ); static sfsistat sample_connect( SMFICTX* ctx, char* connhost, _SOCK_ADDR* connaddr ); static sfsistat sample_helo( SMFICTX* ctx, char* helohost ); static sfsistat sample_envfrom( SMFICTX* ctx, char** fromargs ); static sfsistat sample_envrcpt( SMFICTX* ctx, char** rcptargs ); static sfsistat sample_header( SMFICTX* ctx, char* name, char* value ); static sfsistat sample_eoh( SMFICTX* ctx ); static sfsistat sample_body( SMFICT

                    1 Reply Last reply
                    0
                    • S shrims4u

                      i dint understand??? wat is down voting???

                      J Offline
                      J Offline
                      jeron1
                      wrote on last edited by
                      #11

                      When a response receives a 1 vote (located at the bottom right of the response).

                      S 1 Reply Last reply
                      0
                      • J jeron1

                        When a response receives a 1 vote (located at the bottom right of the response).

                        S Offline
                        S Offline
                        shrims4u
                        wrote on last edited by
                        #12

                        will you please help me??? you understood my problem?

                        1 Reply Last reply
                        0
                        • M molesworth

                          shrims4u wrote:

                          GNU MP: Cannot allocate memory (size=3368600780)

                          I would guess that this is related to the problem. It's tried to allocate 3 Gb of memory, and failed... As to why it's trying to allocate such a big chunk of memory, I couldn't say, but maybe stepping through until you get that error might help pin it down.

                          There are three kinds of people in the world - those who can count and those who can't...

                          S Offline
                          S Offline
                          shrims4u
                          wrote on last edited by
                          #13

                          I dint understand what you want me to do...

                          M 1 Reply Last reply
                          0
                          • S shrims4u

                            I dint understand what you want me to do...

                            M Offline
                            M Offline
                            molesworth
                            wrote on last edited by
                            #14

                            Somewhere the program is trying to allocate a very large amount of memory. This is, I think, the cause of your problem. I haven't used this library/package, but I'm basing my conclusion on the error mesage you posted (see my last reply). What I suggest is that you use a debugging tool to step through the program until it generates that error, and take a close look at exactly what is happening at that point. Or post here exactly which line is causing the problem.

                            There are three kinds of people in the world - those who can count and those who can't...

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups