Segfault using char pointer array

Hi,
My project was just migrated to a SUN M9000 server running Solaris 10 with Sun Studio 12.1. Here is the cc -V output:
Sun C 5.10 SunOS_sparc 2009/06/03
Here is a simple example of code to demonstrate the problem. The compile command used is:
cc -g -m64 filename.c -o filename
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
char *statusMsg[5] = {
"0 ",
"1 ",
"2 ",
"3 ",
"4 " };
printf("Hello %s\n",statusMsg[3]);
sprintf(statusMsg[3],"%s","new3");
printf("Hello %s\n",statusMsg[3]);
return(0);
Output is:
Hello 3
Segmentation Fault
I know it crashes on the sprintf.
dbx output:
dbx crash_dummy
Reading crash_dummy
Reading ld.so.1
Reading libc.so.1
(dbx) run
Running: crash_dummy
(process id 9169)
Reading libc_psr.so.1
Hello 3
signal SEGV (access to address exceeded protections) in _ndoprnt at 0xffffffff7f3ac948
0xffffffff7f3ac948: _ndoprnt+0x295c:    stb      %o4, [%o2]
Current function is main
17 sprintf(statusMsg[3],"%s","new3");
(dbx) where
[1] _ndoprnt(0x1000009aa, 0xffffffff7ffff658, 0x7ffffffefffff62f, 0x1000009b4, 0x1000009b0, 0xffffffff7ffff460), at 0xffffffff7f3ac948
[2] sprintf(0x1000009d0, 0x7fffffff, 0x7ffffc00, 0xffffffff7f54962c, 0xffffffff7f53e000, 0xa), at 0xffffffff7f3ae024
=>[3] main(argc = 1, argv = 0xffffffff7ffff768), line 17 in "crash_dummy.c"
I think it's a bug in libc but I could be wrong. Any help will be appreciated.

I had forgotten about the problem of literal strings being const. Your code has two bugs, and if it ever worked, it worked only by accident.
1. Attempting to overwrite a literal string.
2. Writing beyond the end of an array.
As poster rajp pointed out, you can fix the first problem by compiling with the option -features=no%conststrings. This is not a general solution, but will work with Studio compilers.
The second problem still needs to be addressed. As an implementation detail, Studio compilers in 64-bit mode allocate a literal string on an 8-byte boundary, and allocate the literal strings that initialize the array consecutively. By accident, there are a few padding bytes between the literal strings, allowing you to write extra characters to all but the last of the literal strings. (By another accident, there might be padding after the last literal string.)
To see the bug in action, try writing beyond the accidental padding by writing "new4567890" instead of "new3":
% cat z.c
#include <stdio.h>
#include <string.h>
#define SIZE 5
int main(int argc, char **argv)
    char *statusMsg[SIZE] = {
        "0 ",
        "1 ",
        "2 ",
        "3 ",
        "4 " };
    printf("Hello %s\n",statusMsg[3]);
    printf("Hello %s\n",statusMsg[4]);
    sprintf(statusMsg[3],"%s","new4567890");
    int i;
    for( i=0; i < SIZE; ++i )
        printf("Hello %s\n",statusMsg);
return(0);
% cc -m64 -features=no%conststrings z.c
% ./a.out
Hello 3
Hello 4
Hello 0
Hello 1
Hello 2
Hello new4567890
Hello 90
Notice how the last string was clobbered.
In 32-bit mode, literal strings are allocated on 4-byte boundaries, allowing for even more interesting results. I added an unrelated array of one string, "test", then overwrote statusMsg[3] as before. This time, it clobbers not only statusMsg[4], but "test" as well:#include <stdio.h>
#include <string.h>
#define SIZE 5
int main(int argc, char **argv)
char *statusMsg[SIZE] = {
"0 ",
"1 ",
"2 ",
"3 ",
"4 " };
char *test[1] = { "test" };
printf("Hello %s\n",statusMsg[3]);
printf("Hello %s\n",statusMsg[4]);
printf("Hello %s\n",test[0]);
sprintf(statusMsg[3],"%s","new4567890");
int i;
for( i=0; i < SIZE; ++i )
printf("Hello %s\n",statusMsg[i]);
printf("Hello %s\n",test[0]);
return(0);
213% cc -m32 -features=no%conststrings z.c
214% a.out
Hello 3
Hello 4
Hello test
Hello 0
Hello 1
Hello 2
Hello new4567890
Hello 567890
Hello 90

Similar Messages

  • How to generate localized chars using code point in Solaris 10?

    Hi All,
    Do enybody know how to generate localized chars (for example Japanese) using code points in Solaris 10?
    Like in the following web page:
    http://www.isthisthingon.org/unicode/index.phtml
    Unicode, shift-jis
    U+4e2f 87a3 �N
    U+4e3b 8ee5 ��
    U+4e3c 98a5 �S
    U+4f5c 8dec ��
    Thanks,
    Daniel

    I have found a "Code Point Input Method" tool in the following page:
    http://java.sun.com/products/jfc/tsc/articles/InputMethod/inputmethod.html
    Using this tool user can enter hexadecimal code point and the output is some char.
    But this tool doesn't work. I run this in the follwoing way:
    # java -jar CodePointIM.jar
    After this error message appers:
    Failed to load Main-Class manifest attribute from
    codepointim.jar
    If anybody could help I will be appreciate.
    Regards,
    Daniel

  • Newbie: How to select into a char pointer in Pro*C?

    All Pro*C samples I have seen selects a VARCHAR2 column into a host char array. I want to used a char * instead. However, each time I run the program after successful Pro*C and C compilation, I get a critical error. Can someone share some working code using char * rather than char []? Thanks very much.

    Did you actually allocate space for the data?Funny you mentioned it. Coming from a VB and Java background without C it was not obvious to me that I have to allocate memory after declaring a pointer. But I later found it is necessary. However, even with that I am getting an error. My code (copied from ora-faq and then modified) is like:
    #include <stdio.h>
    #include <sqlca.h>
    void sqlerror();
    EXEC SQL BEGIN DECLARE SECTION;
    char *connstr = "scott/tiger";
    char *db_ename;
    int db_deptno;
    EXEC SQL END DECLARE SECTION;
    int main() {
    db_ename = (char *) malloc(30);
    EXEC SQL WHENEVER SQLERROR DO sqlerror();
         EXEC SQL WHENEVER SQLWARNING CONTINUE;
         EXEC SQL CONNECT :connstr;
         EXEC SQL WHENEVER NOTFOUND GOTO notfound;
         EXEC SQL SELECT ENAME, DEPTNO
              INTO :db_ename, :db_deptno
         FROM EMP
              WHERE EMPNO = 7369;
    found:
         printf("%s is in department %i\n", *db_ename, db_deptno);
         return;
    notfound:
         printf("Employee record not found in database.\n");
         return;
    void sqlerror() {
         printf("Stop Error:\t%25i\n", sqlca.sqlcode);
         return;
    The output is:
    (null) is in department 20
    Did you see anything glaringly wrong?
    Also, in Pro*C, to stored strings, should I use char [], char *, VARCHAR [], or VARCHAR *?
    Thanks,
    Eric

  • How to avoid displaying date,time using Enhancement point.

    Hi friends,
    i have to work on the standard report.
    stadard report displays date,time ,reportname on the top of the page. my requirement is not to display date ,time,reportname.
    they have used the following code,
    WRITE text-001 TO m_line+d_offset(4). " Time
    WRITE sy-timlo USING EDIT MASK '__:__:__' TO m_line+d_offset(08).
    WRITE text-002 TO m_line+d_offset(05). " Date
    WRITE sy-datlo DD/MM/YYYY TO m_line+d_offset(10).
    BY using Enhancement point how to achieve my task.
    kindly help me

    Post Author: jsanzone
    CA Forum: WebIntelligence Reporting
    basham,
    You didn't mention which type of DBMS you are using (i.e. Oracle, MS SQL, MySQL, etc), but in a nutshell using MS SQL here is the principle.  Your time that is recorded in the records is dependent upon a setting in your RDBMS.  For instance, the RDBMS can use the machine time (which would most likely be local time), or it can use the machine time with an offset (to accomodate GMT, for instance).  GMT is factored differently based upon your time zone (and time of year, i.e. DST or EST, etc).  I'm on the east coast so right now my offset is GMT - 5, so if my time right now is 1457, then GMT is 1957.  To get SQL to compute an offset you must take the number of hours to offset and divide by 24 (24 hours in a day) and add that to the record in the database.  For instance, to get GMT right now I would use:
    select cast(getdate()5/24. as char(12))rtrim(convert(char(12),getdate()5/24.,8))' GMT'
    Hope this helps.  If you're on Oracle, then the mathematical principles remain the same, just the formatting will be different.

  • How can I use bullet points on the ipad keyboard?

    When writing Notes, on my iPhone, there is a key for bullet points on the keyboard.  This doesn't seem to exist on the iPad3 keyboard and just wondered if there is any other way to use bullet points on iPad. They are both set to the English UK keyboard.
    Thanks.

    There are many other keyboard keys that are hidden under the standard virtual keyboard keys. They will vary from keyboard to keyboard based on your language settings but this article will point you in the right direction.
    http://www.my-iguru.com/ipad/ipad-hints-tips/ipad-special-characters.php

  • Is there any way to create a circular buffer using the INSERT ARRAY function?

    I saw the example using the REPLACE ARRAY function, but it uses too much CPU power on my laptop. I'm using it for a very sensitive application and saw that the INSERT ARRAY function used much less CPU power.
    I am also not wiring the index on the INSERT ARRAY function so that whatever is read from the buffer is added to the array.
    However, since it is not indexed, I don't know how to set the "write" index back to 0 when it reaches a certain sized array (ie 1000 elements). I was looking for an array property node, but couldn't figure out if one exists for the "current write index".
    Can anyone help?
    Thanks

    I will try to answer this question to my best understanding. I apologize if I interpreted the question wrong.
    You are using the "Insert Array" vi without wiring the index. By doing that, new elements will be added (appended) to the array.
    If you just want to know the current index to stop your acquisition when the array reach certain amount of elements, then you can use the "Array Size" vi to keep track of the size of the growing array. Remember that Array Size = last index + 1 because the index start at zero.
    A second option is that you may want to start over the array when it hits the maximum number of elements that you will allow. If that's the case, then you may want to re-initialize the array to a NULL state by stablishing a condition (for example, when the
    size of the array is 1000, re-initialize). I provided an example attached to this message showing how this can be accomplished. In the example, an 1D array grows in size using the "Insert Array" vi and inserting a random number. When the array reach 4 elements, it is re-initialized. You can expand this example by saving the array first to a file or some other holder before re-initializing the array. The example is in LabVIEW 6.
    Finally, if you want to replace the current values with new values, I think you do not have other choice but to use the "Replace Array" vi.
    Hope this can be of help.
    Regards;
    Enrique Vargas
    www.vartortech.com
    Attachments:
    array_example.vi ‏20 KB

  • How do I link to another anchored page using the point tool?

    Hello
    I am using the point tool (arrow tool) to link to anchors in my current page.  Is there a way of linking to anchors using the tool in another page (in others words one that I am not working on) or does it need to be done manually?
    Thank you
    Jake

    Arrow Tool???  Which arrow tool do you mean?
    Named anchors are deprecated in modern HTML standards.  To create links to content on the same page, set up some Div IDs like this.
    <section id="one">
         Content one goes here...
    </section>
    <section id="two">
         Content two goes here...
    </section>
    <section id="three">
         Content three goes here...
    </section>
    LINKS ON SAME PAGE:
         <a href="#one">Link 1</a>
         <a href="#two">Link 2</a>
         <a href="#three">Link 3</a>
    LINKS ON DIFFERENT PAGE:
         <a href="page_name.html#one">Link 1</a>
         <a href="page_name.html#two">Link 2</a>
         <a href="page_name.html#three">Link 3</a>
    Nancy O.

  • Sharepoint 2013 BI solutions using Performance point services is can be compatible with Analysis server 2008 R2?

    HI
    In Sharepoint 2013 BI solutions using Performance point services  is can be compatible with Analysis server 2008 R2?
    here in sp 2013 i upgraded sp 2010 BI site and when i open dashboard pages they not display the pointers.
    and when i open dashboard designer i was unable to connect Analysis server 2008 R2 where data source exists
    adil

    Hi 
    1) here i migrated a Publishing web applications and bi sites to 2013 sucessfully, this site is enable anonymous access 
    2) Installed SQLSERVER2008_ASADOMD10 
    3) added performance point service application pool account as a db_owner and data read access permission to content database and added permission to unattended service account to SSAS server .
    3) and after i was able to connect to SSAS data soruces.
    here when i open dashboards with administrator user  , this user able to view dashboards
    but when external user able to view dashboards it asking authentication and throws error.
    Log Name:      Application
    Source:        Microsoft-SharePoint Products-PerformancePoint Service
    Date:          3/15/2015 11:59:02 AM
    Event ID:      1101
    Task Category: PerformancePoint Services
    Level:         Error
    Keywords:      
    User:          NT AUTHORITY\IUSR
    Computer:      TSharepint2013.test.com
    Description:
    An exception occurred while rendering a Web control. The following diagnostic information might help to determine the cause of this
     problem:
    Microsoft.PerformancePoint.Scorecards.BpmException:
     The scorecard no longer exists or you do not have permission to view it.
    PerformancePoint Services error code 20700.
    Event Xml:
    <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
      <System>
        <Provider Name="Microsoft-SharePoint Products-PerformancePoint Service" Guid="{A7CD5295-CBBA-4DCA-8B67-D5BE061B6FAE}" />
        <EventID>1101</EventID>
        <Version>15</Version>
        <Level>2</Level>
        <Task>1</Task>
        <Opcode>0</Opcode>
        <Keywords>0x4000000000000000</Keywords>
        <TimeCreated SystemTime="2015-03-15T08:59:02.151586300Z" />
        <EventRecordID>149327</EventRecordID>
        <Correlation ActivityID="{54EDF29C-9842-C025-E404-2869814A5DF0}" />
        <Execution ProcessID="8120" ThreadID="7816" />
        <Channel>Application</Channel>
        <Computer>TSharepint2013.test.com</Computer>
        <Security UserID="S-1-5-17" />
      </System>
      <EventData>
        <Data Name="string1">An exception occurred while rendering a Web control. The following diagnostic information might help to determine the cause of this problem:
    Microsoft.PerformancePoint.Scorecards.BpmException: 
    The scorecard no longer exists or you do not have permission to view it.
    PerformancePoint Services error code 20700.</Data>
      </EventData>
    </Event>
    adil

  • How to use two dimensional array in custom.pll

    HI
    How to use two dimensional arrays in custom.pll
    I tried by the following way .
    type ship_array is table of number index by binary_integer;
    type vc_array is table of ship_array index by binary_integer;
    But I am getting the error as that
    A plsql table may not contain a table or a record with composite fields.
    Please tell me the way how to use them

    which forms version are you using? two dimensional arrays are available in >= 9i if memory serves.
    regards

  • I have adobe acrobat 11 reader writer I want to use the pointer symbol on full screen instead of the hand, iwas able to do it on adobe 10 also when in full screen i can't open attachments unless i right click and open it then it boots me out of full scree

    I have adobe acrobat 11 reader writer I want to use the pointer symbol on full screen instead of the hand, iwas able to do it on adobe 10 also when in full screen i can't open attachments unless i right click and open it then it boots me out of full screen is there any way i can click on the attacment without going out of full screen

    I don't know why the change, but the pointer appears in the same way as it does on the main screen, not a pointer. There does not appear to be anyway to change it. You may want to submit a comment to the Features Requests: Acrobat Feature Requests. I also submitted this as a bug.
    As a clarification, your "adobe acrobat 11 reader writer" is not informative. There is Acrobat 11 and Reader 11. Writer is not a product as such. So you have referred to Acrobat in a contradictory manner. In the future, please just indicate Acrobat 11 or Adobe Acrobat 11.

  • How to use Save Point in Batch Execution of  java transaction updates .

    HI.. ALL
    This is going to be very important to all and if needed history should be rewritten...
    Does any one.... Any one has ever thought of Execution of a Batch - jdbc using Save Points Concept ???
    If this has been ever implemented than I am sorry for my comments..
    If NOT---
    Then any one has idea or code or any thoughts or alternatives for this... This goes like this-->
    If we have a batch file which has All Inserts , Updates and delete statements and we are reading that file in java program and executing the file in a batch execution.. Now java provides only 2 option 'START AND END' for batch execution. But ever dreamed about having Save Points in execution of a batch ?
    You might be thinking why Save points needed in batch? if any one wants to have save points then they can go with normat step by step execution of the jdbc calls Why in batch ?
    Trust me this is very important in various areas of development which will bring an ease of writing the program efficently..
    Does any one have any idea in this regard of having SavePoints in batch execution is highly welcome.
    Thanks
    wahed
    Edited by: wahed on Aug 8, 2008 12:53 AM

    If your database supports SAVEPOINT savepoint_name sql and ROLLBACK [WORK] [ TO [ SAVEPOINT ] savepoint_name] sql, you can try to add those sqls into your sql batch.

  • Unexpected behaviour in case of single value selection by using share point excel service

    If multiple values are passed to pivot table filter using share point excel service, filter is showing “multiple” text on filter. If user expands the filter, the passed values are showing as checked ones.
    Example: ‘India’ and ‘US’ values are passed ‘Country’ filter of pivot table using excel services. Filter is showing ‘multiple’ text on filter and if user expands the filter, ‘India’ and ‘US’ values are showing as checked ones.
    If single value (India) is passed to pivot table filter using share point excel service, filter is showing value text (India) on filter. If user expands the filter, all values are showing as checked ones.
    Ideally it should show ‘India’ as checked one.

    If multiple values are passed to pivot table filter using share point excel service, filter is showing “multiple” text on filter. If user expands the filter, the passed values are showing as checked ones.
    Example: ‘India’ and ‘US’ values are passed ‘Country’ filter of pivot table using excel services. Filter is showing ‘multiple’ text on filter and if user expands the filter, ‘India’ and ‘US’ values are showing as checked ones.
    If single value (India) is passed to pivot table filter using share point excel service, filter is showing value text (India) on filter. If user expands the filter, all values are showing as checked ones.
    Ideally it should show ‘India’ as checked one.

  • What is the use of points earned in apple support communities??

    what is the use of points earned in apple support communities??

    mende1      Valmojado, Toledo, Spain
    This solved my questionRe: How the heck do you get points on your apple support communities account?
    Jul 12, 2013 10:20 PM (in response to BMPU)
    To get points, you have to answer questions. If the user that created the post tested your solution and it worked, he/she (may) give you a "This solved my question". As you can see in the right part of this page, a solved question adds 10 points to your account. If you helped solve the problem, you receive 5 points. On a single thread, the original poster can only give 1 "This solved my question" and 2 "This helped me", so you can receive a total of 20 points.
    With these points, there are levels that you can see in this page > https://discussions.apple.com/static/apple/tutorial/reputation.html When you level up, you receive privileges like report posts, access to Community Calls... The most important level respecting to privileges is the level 6, that gives you access to a special forum called "The Lounge" (where you can talk about anything you want, complain about an user or report problems with the forum), and you also receive gifts from Apple and other privileges

  • How can I use two dimensional array ?

    Could some one show me how to use two dimentional array ?
    I am know how to right a single ...but not two dimentional....please help,
    Thanks,
    Flower
    public class Exam5
    public static void main(String[] args)
    int[][] numbers =
         {     {1,2,3,4,5,6,7,8,9,10},
    {1,2,3,4,5,6,7,8,9,10} };
    for(int i = 1; i < 11; ++i)
    System.out.println(i + "\t" + i * 2 + "\t" + i * 3 + "\t" + i * 4 + "\t" + i * 5 +
    "\t" + i * 6 + "\t" + i * 7 + "\t" + i * 8 + "\t" + i * 9 + "\t" + i * 10);
    Display #
    1     2     3     4     5     6     7     8     9     10
    2     4     6     8     10     12     14     16     18     20
    3     6     9     12     15     18     21     24     27     30
    4     8     12     16     20     24     28     32     36     40
    5     10     15     20     25     30     35     40     45     50
    6     12     18     24     30     36     42     48     54     60
    7     14     21     28     35     42     49     56     63     70
    8     16     24     32     40     48     56     64     72     80
    9     18     27     36     45     54     63     72     81     90
    10     20     30     40     50     60     70     80     90     100

    First, try not to ask someone to do your homework for you and then tell them they are wrong. The code posted may not have been exactly what you were looking for, but it was not wrong because it did exactly what the poster said it would do - print out the multiplication table.
    Second, in the future if you ask specific questions rather than posting code and hoping someone will fix it for you, you may one day be able to complete the assignments on your own.
    Here is code that prints what you want and uses a two dimensional array. Please ask questions if you do not understand the code. You will never learn if you just use someone else's code without taking the time to examine or understand it.
    public class MultiTable{  
        public static void main(String[] args)   { 
            int rows = 10;
            int columns = 10;
            int [][] numbers = new int [rows] [columns];
            for(int j = 0; j < rows; j++)   // for each of 10 rows
                for(int k = 0; k < columns; k++)    // for each of 10 columns
                    numbers[j][k] = (j+1) * (k+1);  // calculate row+1 * col+1
            for (int j = 0; j < rows; j++)  // for each of 10 rows
                for (int k = 0; k < columns; k++)   // for each of 10 columns
                    System.out.print(numbers[j][k]+" ");    // print out result
                    if (numbers[j][k] < 10)     // for single digit numbers
                        System.out.print(" ");  // print extra space for better formatting
                System.out.println();       // skip to next line
    }

  • Re-using the same array on the same page later

    i am using an array to write values to a text file, is there a way to re-use that same array on the same page... after the values has been writen to a file, clear the values and use it again(no need to re-size it), just use it again....

    Thanks. I have a text box that takes a project id and calls a method that gets the data from db and stores it in a vector than i am looping throught the vector and assiging the values to an array and writing each value to a text file. i want to add another text field on there and see if the user wants to pass another project id (passing two) so i have to re-use the arrays again if the text box is not null. don't i need to re-define the arrays again to use them?

Maybe you are looking for

  • How to install the JDI  and what re the prerequisites

    Hi All, Anybody tell me , AS iam new to the SAP NetWeaver How to install the JDI, I have got one document in  sdn . It has given the prerequisite as. Complete installation of DI (CMS, DTR, and CBS) on SAP J2EE Engine Should i install all the above co

  • IPod Updater 2006-06-28 still says Don't Disconnect...

    Hello. This evening I tried updating the firmware of my iPod. Everything seemed to go ok. When this was finished, their still was the "do not disconnect" message on the screen of my iPod. I decided to try the restore button. iPod Updater 2006-06-28 n

  • PKCS#11 and P7M

    Hi, I have to implement an applet for digital signature, the key are in a smart card, I follow the PKCS#11 format to withdraw their. I configure the provider String pkcs11config = "name=SmartCard library=libraryPath"; byte[] pkcs11configBytes = pkcs1

  • Custom Icon

    Hi there Gurus, I need to make a custom cursor. I creat a gif, import it into the cast, click insert media element and select cursor. I then select the gif I want as the cursor, ... however I am not able to check the 32 pixel option. My dilema is ...

  • Problem regarding Combo boxes in JSP

    Hi all, I have two combo boxes on jsp page, i want to fill the second CB by value of first CB, and both CBs have values from Database, i tried it many times but cant got solution plz help me its very urgent and if possible provide code plz thanks